Asynchronous Disk Flushing in Broker Systems

The asynchronous flushing mechanism is implemented through a dedicated service that manages periodic data persistence. Here's the core implemantation: ```

public class AsyncDiskFlusher extends BaseFlushService { private long lastFlushTime = 0;

@Override
public String getServiceIdentifier() {
    return AsyncDiskFlusher.class.getSimpleName();
}

@Override
public void execute() {
    logger.info("Starting {} service", getServiceIdentifier());
    while (!isTerminationRequested()) {
        int flushInterval = getConfiguration().getFlushInterval();
        int minDataBlocks = getConfiguration().getMinimumFlushBlocks();
        int thoroughInterval = getConfiguration().getThoroughFlushInterval();

        long startTime = System.currentTimeMillis();
        
        if (startTime >= (lastFlushTime + thoroughInterval)) {
            lastFlushTime = startTime;
            minDataBlocks = 0; // Remove block limit for comprehensive flush
        }

        try {
            boolean success = dataStorage.commit(minDataBlocks);
            long endTime = System.currentTimeMillis();
            
            if (!success) {
                lastFlushTime = endTime;
                flushScheduler.wakeup(); // Trigger immediate flush
            }

            if (endTime - startTime > 500) {
                logger.info("Flush operation took {} ms", endTime - startTime);
            }
            
            waitBeforeNextFlush(flushInterval);
        } catch (Exception e) {
            logger.error("Error in {} service", getServiceIdentifier(), e);
        }
    }

    // Finalization during shutdown
    boolean completed = false;
    for (int i = 0; i < MAX_RETRIES && !completed; i++) {
        completed = dataStorage.commit(0);
        logger.info("{} shutdown: retry {} {}", 
                   getServiceIdentifier(), i+1, completed ? "Success" : "Failed");
    }
    logger.info("{} service terminated", getServiceIdentifier());
}

}


#### Data Storage Commmit Process

public boolean commit(int minimumBlocks) { boolean result = true; MappedFile currentFile = locateFileByPosition(currentCommitPosition);

if (currentFile != null) {
    int bytesCommitted = currentFile.commit(minimumBlocks);
    long newPosition = currentFile.getFileStartOffset() + bytesCommitted;
    result = newPosition == currentCommitPosition;
    currentCommitPosition = newPosition;
}

return result;

}


#### File Commit Implementation

public int commit(int minimumBlocks) { if (buffer == null) { return writePosition.get(); }

if (canCommit(minimumBlocks)) {
    if (acquireLock()) {
        performCommit(minimumBlocks);
        releaseLock();
    } else {
        logger.warn("Commit failed: lock acquisition failed at position {}", 
                   committedPosition.get());
    }
}

if (buffer != null && pool != null && fileSize == committedPosition.get()) {
    pool.releaseBuffer(buffer);
    buffer = null;
}

return committedPosition.get();

}

protected void performCommit(int minimumBlocks) { int writePosition = this.writePosition.get(); int lastCommitted = this.committedPosition.get();

if (writePosition > lastCommitted) {
    try {
        ByteBuffer dataSlice = buffer.slice();
        dataSlice.position(lastCommitted);
        dataSlice.limit(writePosition);
        
        fileChannel.position(lastCommitted);
        fileChannel.write(dataSlice);
        committedPosition.set(writePosition);
    } catch (IOException e) {
        logger.error("Disk write failure", e);
    }
}

}


#### JVM File Writing Implementation

The native implementation in JVM's FileDispatcherImpl.c: ```

#define pwrite64 pwrite // Platform-specific write operation

JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fileDescriptor,
                                          jlong bufferAddress, jint length, jlong position) {
    int fileHandle = getFdValue(env, fileDescriptor);
    void *buffer = (void *)jlong_to_ptr(bufferAddress);
    
    // Execute platform-specific write operation
    return convertReturnCode(env, pwrite64(fileHandle, buffer, length, position), JNI_FALSE);
}

Tags: java AsynchronousIO filechannel MappedByteBuffer

Posted on Wed, 09 Sep 2026 16:28:50 +0000 by sinbad