ID | IEP-28 |
Author | |
Sponsor | |
Created | 31-Oct-2018 |
Status | |
Motivation
The Apache Ignite cluster balance procedure with enabled persitence currently doesn't utilize network and storage device throughout to its full extent. The balance procedure processes cache data entries one by one which is not efficient enough for the cluster with enabled persistence.
Competitive Analysis
Profiling current balancing procedure
Rebalance procedure optimizations
Possible partition file sending approaches
The Apache Ignite can support cache rebalancing as transferring partition files using zero copy algorithm [1] based on an extension of communication SPI and Java NIO API. When the partition file has been transferred to the demander node there are two possible approaches can be implemented to preload entries from particular partition file.
Hot swap cache data storage
The demander node will initialize a preloaded partition file as a new PageStore, make this storage up-to-date by applying previously saved async cache operations and then hot swap it under the checkpoint write lock.
Disadvantages:
- A long and complex index reduild procedure that requires the development of additional crash recovery guarantees.
The index rebuild procedure starts immediately when the partition file is fully received from the supplier node. If the node crashes in the middle of the rebuilding index process it will have an inconsistent index state at the further node startup. To avoid this a new index-undo WAL record must be logged within rebuilding and used on node start to remove previously added index records.
Preload entries from loaded partition file
The demander node will use a preloaded patition file as a new source of cache data entries to load.
Disadvantages:
- The approach will require a new temporary FilePageStore to be initialized. It must be created as a part of the temporary cache group or in the separate temporary data region to provide reusing machinery of iteration over the full partition file.
Proposed Changes (Hot swap)
Process Overview
In the process of balancing data:
- demaner (receiver of partition files)
- supplier (sender of partition files).
The whole process is described in terms of rebalance single cache group and partition files would be rebalanced one-by-one:
- The demander node prepares the set of IgniteDhtDemandedPartitionsMap#full cache partitions to fetch;
- The demander node checks compatibility version (for example, 2.8) and starts recording all incoming cache updates to the new special storage – the temporary WAL storage;
- The demander node sends the GridDhtPartitionDemandMessage to the supplier node as usual;
- The supplier node receives GridDhtPartitionDemandMessage and starts the new checkpoint process and fixes cache partition file sizes;
- The supplier node creates an empty temporary file with .delta (e.g. part-0.bin.delta file) postfix for each cache partition file (in the same cache working directory or another configured);
- The supplier node starts tracking each pageId write attempt to these partition files
- When the write attempt happens, the thread that caused it reads the original copy of this page from the partition and flushes it to the corresponding .delta file;
- After it the thread writes the changed page data to the partition file;
- The supplier waits the checkpoint process ends;
- On the supplier for each cache partition file
- The process opens the partition file in read-only mode;
- Starts sending partition file (as it is with any concurrent writes) by chunks of predefined constant size (multiple of PageMemory size);
- After the partition file sent it starts sending corresponding .delta file;
- The demander node starts to listen to new type of incoming connections (a socket channel created event) from the supplier node;
- When the appropriate connection established the demander node for each cache partition file
- Receives file metadata information (corresponding cache group identifier, cache partition file name, file size)
- Writes data from the socket to the particular cache partition file from the beginning of the file
- After the original cache partition file received the node starts receiving corresponding .delta file
- The node reads data from the socket by chunks of PageMemory size and applies each received pageId to the partition file
- When the demander node receives the whole cache partition file
- The node begins the rebuild secondary indexes procedure over received partition file
- After it the thread begins to apply for data entries from the beginning of WAL-temporary storage;
- All async operations corresponding to this partition file still write to the end of temporary WAL;
- At the moment of WAL-temporary storage is ready to be empty
- Start the first checkpoint;
- Wait for the first checkpoint ends and own the cache partition;
- All operations now are switched to the partition file instead of writing to the temporary WAL;
- Schedule the temporary WAL storage deletion;
- The supplier node deletes all temporary files;
Components
In terms of a high-level abstraction, Apache Ignite must support the features described below.
File transfer between nodes
The node partition preloader machinery download cache partition files from cluster nodes which owns desired partitions (the zero copy algorithm [1] assume to be used by default). To achieve this, the file transmission process must be implemented at Apache Ignite over Communication SPI.
CommunicationSpi
IThe Comminication SPI must support to:
- opening channel connections to a remote node to an arbitrary topic (GridTopic is used) with initial meta information;
- listening incoming channel connections and handling them by registered handlers;
- an arbitrary set of channel parameters on connection handshake (some initial Message assumed to be used);
API
public interface CommunicationListenerEx<T extends Serializable> extends EventListener {
/**
* @param nodeId Remote node id.
* @param initMsg Init channel message.
* @param channel Locally created channel endpoint.
*/
public void onChannelOpened(UUID nodeId, Message initMsg, Channel channel);
}
GridIoManager
IO manager must support to:
- different approaches of incoming data handling: CHUNK (read channel into ByteBuffer), FILE (zero-copy approach)
- send and receive data by chunks of predefined size with storing intermediate results;
- reestablishing connection between nodes if an error occurs and continue file sending\receiving;
- limiting connection bandwidth at runtime;
API
public interface TransmissionHandler {
/**
* @param err The err of fail handling process.
*/
public void onException(UUID nodeId, Throwable err);
/**
* @param nodeId Remote node id from which request has been received.
* @param fileMeta File meta info.
* @return Absolute pathname denoting a file.
*/
public String filePath(UUID nodeId, TransmissionMeta fileMeta);
/**
* <em>Chunk handler</em> represents by itself the way of input data stream processing.
* It accepts within each chunk a {@link ByteBuffer} with data from input for further processing.
*
* @param nodeId Remote node id from which request has been received.
* @param initMeta Initial handler meta info.
* @return Instance of chunk handler to process incoming data by chunks.
*/
public Consumer<ByteBuffer> chunkHandler(UUID nodeId, TransmissionMeta initMeta);
/**
* <em>File handler</em> represents by itself the way of input data stream processing. All the data will
* be processed under the hood using zero-copy transferring algorithm and only start file processing and
* the end of processing will be provided.
*
* @param nodeId Remote node id from which request has been received.
* @param initMeta Initial handler meta info.
* @return Intance of read handler to process incoming data like the {@link FileChannel} manner.
*/
public Consumer<File> fileHandler(UUID nodeId, TransmissionMeta initMeta);
}
public class TransmissionSender implements Closeable {
/**
* @param file Source file to send to remote.
* @param params Additional transfer file description keys.
* @param plc The policy of handling data on remote.
* @throws IgniteCheckedException If fails.
*/
public void send(
File file,
Map<String, Serializable> params,
TransmissionPolicy plc
) throws IgniteCheckedException, InterruptedException, IOException {
send(file, 0, file.length(), params, plc);
}
/**
* @param file Source file to send to remote.
* @param plc The policy of handling data on remote.
* @throws IgniteCheckedException If fails.
*/
public void send(
File file,
TransmissionPolicy plc
) throws IgniteCheckedException, InterruptedException, IOException {
send(file, 0, file.length(), new HashMap<>(), plc);
}
/**
* @param file Source file to send to remote.
* @param offset Position to start trasfer at.
* @param cnt Number of bytes to transfer.
* @param params Additional transfer file description keys.
* @param plc The policy of handling data on remote.
* @throws IgniteCheckedException If fails.
*/
public void send(
File file,
long offset,
long cnt,
Map<String, Serializable> params,
TransmissionPolicy plc
) throws IgniteCheckedException, InterruptedException, IOException {
// Impl.
}
}
Copy partition on the fly
Checkpointer
When the supplier node receives the cache partition file demand request it will send the file over the CommunicationSpi. The cache partition file can be concurrently updated by checkpoint thread during its transmission. To guarantee the file consistency Сheckpointer must use Copy-on-Write [3] tehnique and save a copy of updated chunk into the temporary file.
Apply partition on the fly
Catch-up temporary WAL
While the demander node is in the partition file transmission state it must save sequentially all cache entries corresponding to the MOVING partition into a new temporary storage. These entries will be applied later one by one on the newly received cache partition file. All asynchronous operations will be enrolled to the end of temporary storage during storage reads until it becomes fully read. The file-based FIFO approach assumes to be used by this process.
The temporary storage is chosen to be WAL-based. The storage must support to:
- Unlimited number of WAL-files to store temporary data records;
- Iterating over stored data records during an asynchronous writer thread insert new records;
- WAL-per-partiton approach is need to be used;
- Write operations to storage must have higher priority over read operations;
Expected problems to be solved
- We must stop updating indexes on demander when the data is ready to be transferred from the supplier node. All async cache updates on demander must not cause the index update;
- The previous partition metadata page and all stored meta information must be destroyed in PageMemory and restored from the new partition file;
Rebuild indexes
The node is ready to become partition owner when partition data is rebalanced and cache indexes are ready. For the message-based cluster rebalancing approach indexes are rebuilding simultaneously with cache data loading. For the file-based rebalancing approach, the index rebuild procedure must be finished before the partition state is set to the OWNING state.
Failover and Recovery
Apache Ignite doesn't provide any recovery guarantees for the partitions with the MOVING state. The cache partitions will be fully loaded when the next rebalance procedure occurs.
Topology change
Each topology change event JOIN/LEFT/FAILED may or may not change cache affinity assignments of currently rebalacning caches. If assignments is not changed and the node is still needs partitions being rebalanced we can continue the current rebalance process (see for details IGNITE-7165).
Activation\deactivation
The rebalance procedure will be stopped if the deactivation event occurs. The single partition will be lost and will be preloaded on the next cluster rebalancing.
Unstable connection
A new connection must be established and the download process of partition file must be continued from the last successfully send cache partition chunk.
Crash recovery
To provide basic recovery guarantees we must to:
- Start the checkpoint process when the temporary WAL becomes empty;
- Wait for the first checkpoint ends and set OWNING status to partition;
Recovery from different stages:
- Supplier crashes when sending partition
- Demander crashes when receiving partition
- Demander crashes when applying temp WAL
Phase-2
The SSL must be disabled to take an advantage of Java NIO zero-copy file transmission using FileChannel#transferTo method. If we need to use SSL the file must be splitted on chunks the same way to send them over the socket channel with ByteBuffer. As the SSL engine generally needs a direct ByteBuffer to do encryption we can't avoid copying buffer payload from the kernel level to the application level.
Risks and Assumptions
A few notes can be mentioned:
- Zero-copy limitations – If operating system does not support zero copy, sending a file with FileChannel#transferTo might fail or yield worse performance. For example, sending a large file doesn't work well enough on Windows;
- Writing WAL io wait time – Under the heavy load of partition file transmission, writing to the temporary WAL storage may be slowing down. Since the loss of data of temporary WAL storage has no risks we can consider store the whole storage into the memory.
Discussion Links
http://apache-ignite-developers.2346864.n4.nabble.com/DISCUSSION-Design-document-Rebalance-caches-by-transferring-partition-files-td38388.html
Reference Links
- Zero Copy I: User-Mode Perspective – https://www.linuxjournal.com/article/6345
- Example: Efficient data transfer through zero copy – https://www.ibm.com/developerworks/library/j-zerocopy/index.html
- Copy-on-write – https://en.wikipedia.org/wiki/Copy-on-write
Tickets
key |
summary |
type |
updated |
assignee |
reporter |
priority |
status |
resolution |
JQL and issue key arguments for this macro require at least one Jira application link to be configured
|