Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix:pd) become a leader before the addLeaderStateListener #1062

Merged
merged 1 commit into from
Jan 3, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
*/
package com.alipay.sofa.jraft.rhea;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

import com.alipay.sofa.jraft.conf.Configuration;
import com.alipay.sofa.jraft.rhea.options.RegionEngineOptions;
import com.alipay.sofa.jraft.rhea.options.StoreEngineOptions;
import com.alipay.sofa.jraft.rhea.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -128,11 +133,14 @@ public synchronized boolean init(final PlacementDriverServerOptions opts) {
final RheaKVStoreOptions rheaOpts = opts.getRheaKVStoreOptions();
Requires.requireNonNull(rheaOpts, "opts.rheaKVStoreOptions");
this.rheaKVStore = new DefaultRheaKVStore();
this.placementDriverService = new DefaultPlacementDriverService(this.rheaKVStore);
// Set up a listener before becoming a leader
this.rheaKVStore.addLeaderStateListener(getPdReginId(rheaOpts),
((DefaultPlacementDriverService) this.placementDriverService));
if (!this.rheaKVStore.init(rheaOpts)) {
LOG.error("Fail to init [RheaKVStore].");
return false;
}
this.placementDriverService = new DefaultPlacementDriverService(this.rheaKVStore);
if (!this.placementDriverService.init(opts)) {
LOG.error("Fail to init [PlacementDriverService].");
return false;
Expand All @@ -147,13 +155,42 @@ public synchronized boolean init(final PlacementDriverServerOptions opts) {
throw new IllegalArgumentException("Only support single region for [PlacementDriverServer]");
}
this.regionEngine = regionEngines.get(0);
this.rheaKVStore.addLeaderStateListener(this.regionEngine.getRegion().getId(),
((DefaultPlacementDriverService) this.placementDriverService));
addPlacementDriverProcessor(storeEngine.getRpcServer());
LOG.info("[PlacementDriverServer] start successfully, options: {}.", opts);
return this.started = true;
}

private long getPdReginId(final RheaKVStoreOptions rheaOpts) {
StoreEngineOptions storeEngineOptions = rheaOpts.getStoreEngineOptions();
Requires.requireNonNull(storeEngineOptions, "storeEngineOptions");
List<RegionEngineOptions> rOptsList = storeEngineOptions.getRegionEngineOptionsList();
if (rOptsList == null || rOptsList.isEmpty()) {
return Constants.DEFAULT_REGION_ID;
}
List<RegionEngineOptions> filteredOptsList = new ArrayList<>();
for (RegionEngineOptions rOpts : rOptsList) {
if (inConfiguration(rOpts.getServerAddress().toString(), rOpts.getInitialServerList())) {
filteredOptsList.add(rOpts);
}
}
if (filteredOptsList.size() > 1) {
throw new IllegalArgumentException("Only support single region for [PlacementDriverServer]");
}
return rOptsList.get(0).getRegionId();
}

private boolean inConfiguration(final String curr, final String all) {
final PeerId currPeer = new PeerId();
if (!currPeer.parse(curr)) {
return false;
}
final Configuration allConf = new Configuration();
if (!allConf.parse(all)) {
return false;
}
return allConf.contains(currPeer) || allConf.getLearners().contains(currPeer);
}

@Override
public synchronized void shutdown() {
if (!this.started) {
Expand Down
Loading