Status

Current state"Under Discussion"

Discussion thread: [link]

JIRA: [link]

Released: 0.5.0

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

When using the distributedlog-client module writing data through distributedlog-service, the resulting state of the log stream is hard to predict. It is possible for a distributedlog client to end up writing duplicated records to a log stream, or having unexpected arriving order on records and create an inconsistent state.

This proposal here proposes adding a simple write primitive to the thin distributedlog client. The write primitive is a CAS (check-and-set) style operation. The client will attempt to write a record with a provided SN (sequence number) - if the provided SN does not match the SN of last written record, distributedlog-service should refuse serving the write request. The CAS style write lets the client confirm the current state of a log stream before going ahead with the write. The SN can be the system generated DLSN, the monotonically increasing Sequence ID or the user provided transaction id.

The CAS style write is useful in following use cases:

  • It will be very useful for bootstrapping a state machine – for example, when a state machine is restarted, it has to bootstrap from last snapshot and after the state machine is caught up with the latest commit, it completes bootstrap and writes new commits to the same log stream. The CAS style write can guarantee that the state machine will consume all the records at the moment it does the first successful write.
  • It will be useful for 'read-transform-write' tasks – for example, when a task is reading data from a HDFS file and loading the data into a log stream, the CAS style write can CAS on SN to guarantee the data will be copied just once.
  • It will be very useful for avoiding two concurrent writes to a log stream at the same time. As it would guarantee at least one of them will fail.

Comparing this with providing transactional support, it is much light weight.

Public Interfaces

The proposal requires a few additions to the protocol and the public interface.

The protocol changes are listed as below:

  • Add a new Struct 'SN'

    struct SN {
    	1: optional DLSN dlsn;
    	2: optional long transactionId;
    	3: optional long sequenceId;
    }
  • Add an optional field 'casSN' to WriteContext

    struct WriteContext {
    	...
    	4: optional SN casSN;
    }
  • Add a new status code

    CAS_WRITE_EXCEPTION = 525

The public API change will be:

Core Library Change:

  • Future<DLSN> write(LogRecord record, DLSN expectedDLSN);
  • Future<DLSN> write(LogRecord record, long expectedTransactionID);

Thin Client Change:

  • Future<DLSN> write(String stream, ByteBuffer data, DLSN expectedDLSN);
  • Future<DLSN> write(String stream, ByteBuffer data, long expectedTransactionID);

Proposed Changes

Currently the writer already have tracked the last transaction id and known of the entry id when writing records to a log segment. We can easily compare the provided 'expectedDLSN' and 'expectedTransactionID' when writing the records. It is a trivial change.

Compatibility, Deprecation, and Migration Plan

  • This change only adds new API to the client. So the new server and the old client are backward compatible. It doesn't require special instructions for upgrading servers.

Rejected Alternatives

The other two DPs: DP-2 - Epoch Write Support and DP-1 - DistributedLog Transaction Support have quite a lot of overlaps with the use cases here. For example, a full fledged transaction would be able to support these use cases. However, this proposal here has provided a much trivial/lightweight extension to existing API and it imposes little to no runtime cost in memory or time.

  • No labels