Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions fluss-common/src/main/java/org/apache/fluss/utils/FlussPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public class FlussPaths {
/** Suffix of a producer offsets file. */
private static final String PRODUCER_OFFSETS_FILE_SUFFIX = ".offsets";

/** The directory name for storing rebalance task snapshots. */
private static final String REMOTE_REBALANCE_DIR_NAME = "rebalance";

/** Suffix of a rebalance task snapshot file. */
private static final String REBALANCE_TASK_FILE_SUFFIX = ".task";

private static final String REMOTE_LEASE_DIR_NAME = "lease";

// ----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -909,6 +915,19 @@ public static FsPath remoteProducerOffsetsPath(
uuid + PRODUCER_OFFSETS_FILE_SUFFIX);
}

/** Returns a unique remote file path for a rebalance task snapshot. */
public static FsPath remoteRebalanceTaskPath(
String remoteDataDir, String rebalanceId, UUID fileId) {
return new FsPath(
new FsPath(remoteRebalanceDir(remoteDataDir), rebalanceId),
fileId + REBALANCE_TASK_FILE_SUFFIX);
}

/** Returns the remote directory containing all rebalance task snapshots. */
public static FsPath remoteRebalanceDir(String remoteDataDir) {
return new FsPath(remoteDataDir, REMOTE_REBALANCE_DIR_NAME);
}

// ----------------------------------------------------------------------------------------
// Remote Paths for common use
// ----------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.apache.fluss.rpc.messages.ControlledShutdownResponse;
import org.apache.fluss.rpc.messages.ListRebalanceProgressResponse;
import org.apache.fluss.rpc.messages.PbCommitLakeTableSnapshotRespForTable;
import org.apache.fluss.rpc.messages.RebalanceResponse;
import org.apache.fluss.rpc.messages.RemoveServerTagByRackResponse;
import org.apache.fluss.rpc.messages.RemoveServerTagResponse;
import org.apache.fluss.rpc.protocol.ApiError;
Expand Down Expand Up @@ -101,6 +100,7 @@
import org.apache.fluss.server.coordinator.event.NotifyLeaderAndIsrRequestContext;
import org.apache.fluss.server.coordinator.event.NotifyLeaderAndIsrResponseReceivedEvent;
import org.apache.fluss.server.coordinator.event.RebalanceEvent;
import org.apache.fluss.server.coordinator.event.RebalancePersistenceCompleteEvent;
import org.apache.fluss.server.coordinator.event.ReclaimTableBulkLoadMetadataEvent;
import org.apache.fluss.server.coordinator.event.ReconcileRebalanceTaskEvent;
import org.apache.fluss.server.coordinator.event.RecoverRebalanceEvent;
Expand Down Expand Up @@ -253,7 +253,13 @@ public CoordinatorEventProcessor(
this.coordinatorEventManager = new CoordinatorEventManager(this, coordinatorMetricGroup);
this.rebalanceManager =
new RebalanceManager(
this, zooKeeperClient, coordinatorEventManager, clock, conf, scheduler);
this,
zooKeeperClient,
coordinatorEventManager,
clock,
conf,
scheduler,
ioExecutor);
CoordinatorRequestBatch replicaRequestBatch =
new CoordinatorRequestBatch(
coordinatorChannelManager,
Expand Down Expand Up @@ -664,8 +670,14 @@ public void startup() {
scheduler, coordinatorEventManager, bulkLoadResultRetentionMs);
}

/** Starts loading the persisted rebalance task without blocking coordinator startup. */
public void startRebalanceRecovery() {
rebalanceManager.startRecovery();
}

public void shutdown() {
bulkLoadManager.stopMetadataReclamation();
rebalanceManager.stopRecovery();
clearOfflineLeaderRetryTask();
// Cancel periodic event producers before stopping the event loop.
rebalanceManager.stopTimeoutChecker();
Expand Down Expand Up @@ -1126,27 +1138,36 @@ public void process(CoordinatorEvent event) {
() -> processRemoveServerTagByRack(removeServerTagByRackEvent));
} else if (event instanceof RebalanceEvent) {
RebalanceEvent rebalanceEvent = (RebalanceEvent) event;
completeFromCallable(
rebalanceEvent.getRespCallback(), () -> processRebalance(rebalanceEvent));
try {
processRebalance(rebalanceEvent);
} catch (Throwable t) {
rebalanceEvent.getRespCallback().completeExceptionally(t);
}
} else if (event instanceof RecoverRebalanceEvent) {
RebalanceTask rebalanceTask = ((RecoverRebalanceEvent) event).getRebalanceTask();
assignmentPersistenceByBucket.clear();
rebalanceManager.recoverRebalance(rebalanceTask);
boolean recovered = rebalanceManager.recoverRebalance(rebalanceTask);
// The coordinator context was rebuilt from ZooKeeper before recovery was submitted,
// so its current assignments are already confirmed without another per-bucket read.
for (TableBucket tableBucket : rebalanceTask.getExecutePlan().keySet()) {
if (rebalanceManager.getRebalancePlanForBucket(tableBucket) != null
&& !coordinatorContext.getAssignment(tableBucket).isEmpty()) {
assignmentPersistenceByBucket.put(
tableBucket,
new ArrayList<>(coordinatorContext.getAssignment(tableBucket)));
if (recovered) {
for (TableBucket tableBucket : rebalanceTask.getExecutePlan().keySet()) {
if (rebalanceManager.getRebalancePlanForBucket(tableBucket) != null
&& !coordinatorContext.getAssignment(tableBucket).isEmpty()) {
assignmentPersistenceByBucket.put(
tableBucket,
new ArrayList<>(coordinatorContext.getAssignment(tableBucket)));
}
}
}
} else if (event instanceof CancelRebalanceEvent) {
CancelRebalanceEvent cancelRebalanceEvent = (CancelRebalanceEvent) event;
completeFromCallable(
cancelRebalanceEvent.getRespCallback(),
() -> processCancelRebalance(cancelRebalanceEvent));
try {
processCancelRebalance(cancelRebalanceEvent);
} catch (Throwable t) {
cancelRebalanceEvent.getRespCallback().completeExceptionally(t);
}
} else if (event instanceof RebalancePersistenceCompleteEvent) {
((RebalancePersistenceCompleteEvent) event).complete();
} else if (event instanceof CheckRebalanceTimeoutEvent) {
rebalanceManager.checkTimeout();
} else if (event instanceof ReconcileRebalanceTaskEvent) {
Expand Down Expand Up @@ -2004,7 +2025,7 @@ private void removeServerTags(List<Integer> serverIds, ServerTag serverTag) {
coordinatorContext.getServerTags());
}

private RebalanceResponse processRebalance(RebalanceEvent rebalanceEvent) {
private void processRebalance(RebalanceEvent rebalanceEvent) {
// Recovery may still be loading or queued behind this request. Persisting a new task before
// the recovered task is applied would let the delayed recovery overwrite the local state.
checkRebalanceRecoveryComplete();
Expand All @@ -2022,12 +2043,6 @@ private RebalanceResponse processRebalance(RebalanceEvent rebalanceEvent) {
// 1. generate rebalance plan.
rebalanceTask =
rebalanceManager.generateRebalanceTask(rebalanceEvent.getGoalsByPriority());

// 2. execute rebalance plan.
Map<TableBucket, RebalancePlanForBucket> executePlan = rebalanceTask.getExecutePlan();
assignmentPersistenceByBucket.clear();
zooKeeperClient.registerRebalanceTask(rebalanceTask);
rebalanceManager.registerRebalance(rebalanceTask.getRebalanceId(), executePlan);
} catch (Exception e) {
throw new RebalanceFailureException(
String.format(
Expand All @@ -2036,19 +2051,50 @@ private RebalanceResponse processRebalance(RebalanceEvent rebalanceEvent) {
e);
}

LOG.info(
"Generate Rebalance plan rebalance id {} with {} ms.",
rebalanceTask.getRebalanceId(),
System.currentTimeMillis() - startTime);
return makeRebalanceResponse(rebalanceTask.getRebalanceId());
// Persist before exposing the task locally. Remote storage and ZooKeeper I/O run on the
// coordinator I/O executor; the completion is applied by the event thread.
rebalanceManager.persistRebalanceTask(
rebalanceTask,
failure -> {
if (failure != null) {
rebalanceEvent
.getRespCallback()
.completeExceptionally(
new RebalanceFailureException(
"Failed to persist and execute rebalance plan.",
failure));
return;
}
try {
assignmentPersistenceByBucket.clear();
rebalanceManager.registerRebalance(
rebalanceTask.getRebalanceId(), rebalanceTask.getExecutePlan());
LOG.info(
"Generate Rebalance plan rebalance id {} with {} ms.",
rebalanceTask.getRebalanceId(),
System.currentTimeMillis() - startTime);
rebalanceEvent
.getRespCallback()
.complete(makeRebalanceResponse(rebalanceTask.getRebalanceId()));
} catch (Throwable t) {
rebalanceEvent.getRespCallback().completeExceptionally(t);
}
});
}

private CancelRebalanceResponse processCancelRebalance(
CancelRebalanceEvent cancelRebalanceEvent) {
private void processCancelRebalance(CancelRebalanceEvent cancelRebalanceEvent) {
checkRebalanceRecoveryComplete();
CancelRebalanceResponse response = new CancelRebalanceResponse();
rebalanceManager.cancelRebalance(cancelRebalanceEvent.getRabalanceId());
return response;
rebalanceManager.cancelRebalance(
cancelRebalanceEvent.getRabalanceId(),
failure -> {
if (failure == null) {
cancelRebalanceEvent
.getRespCallback()
.complete(new CancelRebalanceResponse());
} else {
cancelRebalanceEvent.getRespCallback().completeExceptionally(failure);
}
});
}

private ListRebalanceProgressResponse processListRebalanceProgress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -378,6 +379,10 @@ protected void initCoordinatorLeader() throws Exception {
dynamicConfigManager.pauseListening();

createDefaultDatabase();

// Rebalance plans can be large and may live in remote storage. Recover them only
// after the leader's base services are ready, and never block leader startup on I/O.
coordinatorEventProcessor.startRebalanceRecovery();
}
}

Expand Down Expand Up @@ -496,7 +501,9 @@ private void unregisterCoordinatorLeader() throws Exception {
private CoordinatorAddress buildCoordinatorAddress() {
List<Endpoint> bindEndpoints = rpcServer.getBindEndpoints();
return new CoordinatorAddress(
this.serverId, Endpoint.loadAdvertisedEndpoints(bindEndpoints, conf));
this.serverId,
Endpoint.loadAdvertisedEndpoints(bindEndpoints, conf),
Collections.singleton(CoordinatorAddress.REMOTE_REBALANCE_TASK_FEATURE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.fluss.server.coordinator.event;

/** Returns completion of asynchronous rebalance persistence to the coordinator event thread. */
public final class RebalancePersistenceCompleteEvent implements CoordinatorEvent {

private final Runnable completion;

/** Creates an event for an internally supplied persistence completion. */
public RebalancePersistenceCompleteEvent(Runnable completion) {
this.completion = completion;
}

/** Applies the persistence result on the coordinator event thread. */
public void complete() {
completion.run();
}
}
Loading
Loading