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

Refactor/design smells #1098

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class CounterServer {
private CounterStateMachine fsm;

public CounterServer(final String dataPath, final String groupId, final PeerId serverId,
final NodeOptions nodeOptions) throws IOException {
final NodeOptions nodeOptions, final CounterServiceProvider counterServiceProvider)
throws IOException {
// init raft data path, it contains log,meta,snapshot
FileUtils.forceMkdir(new File(dataPath));

Expand All @@ -57,7 +58,7 @@ public CounterServer(final String dataPath, final String groupId, final PeerId s
CounterGrpcHelper.setRpcServer(rpcServer);

// register business processor
CounterService counterService = new CounterServiceImpl(this);
CounterService counterService = counterServiceProvider.getCounterService();
rpcServer.registerProcessor(new GetValueRequestProcessor(counterService));
rpcServer.registerProcessor(new IncrementAndGetRequestProcessor(counterService));
// init state machine
Expand Down Expand Up @@ -116,6 +117,8 @@ public static void main(final String[] args) throws IOException {
final String serverIdStr = args[2];
final String initConfStr = args[3];

CounterServiceProvider counterServiceProvider = new CounterServiceImpl(null);

final NodeOptions nodeOptions = new NodeOptions();
// for test, modify some params
// set election timeout to 1s
Expand All @@ -137,7 +140,9 @@ public static void main(final String[] args) throws IOException {
nodeOptions.setInitialConf(initConf);

// start raft server
final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions);
final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions,
counterServiceProvider);
// ((CounterServiceImpl) counterServiceProvider).counterServer = counterServer;
System.out.println("Started counter server at port:"
+ counterServer.getNode().getNodeId().getPeerId().getPort());
// GrpcServer need block to prevent process exit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* @author likun (saimu.msm@antfin.com)
*/
public class CounterServiceImpl implements CounterService {
public class CounterServiceImpl implements CounterService, CounterServiceProvider {
private static final Logger LOG = LoggerFactory.getLogger(CounterServiceImpl.class);

private final CounterServer counterServer;
Expand All @@ -47,6 +47,11 @@ public CounterServiceImpl(CounterServer counterServer) {
this.readIndexExecutor = createReadIndexExecutor();
}

@Override
public CounterService getCounterService() {
return this;
}

private Executor createReadIndexExecutor() {
final StoreEngineOptions opts = new StoreEngineOptions();
return StoreEngineHelper.createReadIndexExecutor(opts.getReadIndexCoreThreads());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 com.alipay.sofa.jraft.example.counter;

public interface CounterServiceProvider {
CounterService getCounterService();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 com.alipay.sofa.jraft.rhea.storage;

/**
* Seperation of concerns for batch delete operations from {@link BatchRawKVStore}.
*
* @author jiachun.fjc + JAYDIPSINH27
*/
public class BatchRawDeleteOperations<T> {
private final BatchRawKVStore<T> store;

public BatchRawDeleteOperations(BatchRawKVStore<T> store) {
this.store = store;
}

public void batchDelete(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
store.delete(kvState.getOp().getKey(), kvState.getDone());
}
}

public void batchDeleteRange(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
store.deleteRange(op.getStartKey(), op.getEndKey(), kvState.getDone());
}
}

public void batchDeleteList(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
store.delete(kvState.getOp().getKeys(), kvState.getDone());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,36 @@
*/
public abstract class BatchRawKVStore<T> extends BaseRawKVStore<T> {

private final BatchRawPutOperations<T> batchRawPutOperations;
private final BatchRawDeleteOperations<T> batchRawDeleteOperations;

public BatchRawKVStore() {
this.batchRawPutOperations = new BatchRawPutOperations<>(this);
this.batchRawDeleteOperations = new BatchRawDeleteOperations<>(this);
}

public void batchPut(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
put(op.getKey(), op.getValue(), kvState.getDone());
}
batchRawPutOperations.batchPut(kvStates);
}

public void batchPutIfAbsent(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
putIfAbsent(op.getKey(), op.getValue(), kvState.getDone());
}
batchRawPutOperations.batchPutIfAbsent(kvStates);
}

public void batchPutList(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
put(kvState.getOp().getEntries(), kvState.getDone());
}
batchRawPutOperations.batchPutList(kvStates);
}

public void batchDelete(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
delete(kvState.getOp().getKey(), kvState.getDone());
}
batchRawDeleteOperations.batchDelete(kvStates);
}

public void batchDeleteRange(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
deleteRange(op.getStartKey(), op.getEndKey(), kvState.getDone());
}
batchRawDeleteOperations.batchDeleteRange(kvStates);
}

public void batchDeleteList(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
delete(kvState.getOp().getKeys(), kvState.getDone());
}
batchRawDeleteOperations.batchDeleteList(kvStates);
}

public void batchGetSequence(final KVStateOutputList kvStates) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 com.alipay.sofa.jraft.rhea.storage;

/**
* Seperation of concerns for batch put operations from {@link BatchRawKVStore}.
*
* @author jiachun.fjc + JAYDIPSINH27
*/
public class BatchRawPutOperations<T> {
private final BatchRawKVStore<T> store;

public BatchRawPutOperations(BatchRawKVStore<T> store) {
this.store = store;
}

public void batchPut(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
store.put(op.getKey(), op.getValue(), kvState.getDone());
}
}

public void batchPutIfAbsent(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
final KVOperation op = kvState.getOp();
store.putIfAbsent(op.getKey(), op.getValue(), kvState.getDone());
}
}

public void batchPutList(final KVStateOutputList kvStates) {
for (int i = 0, l = kvStates.size(); i < l; i++) {
final KVState kvState = kvStates.get(i);
store.put(kvState.getOp().getEntries(), kvState.getDone());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
*/
public class AtomicServer {

private static final Logger LOG = LoggerFactory.getLogger(AtomicServer.class);
private static final Logger LOG = LoggerFactory.getLogger(AtomicServer.class);

private TreeMap<Long, AtomicRangeGroup> nodes = new TreeMap<>();
private TreeMap<Long, String> groups = new TreeMap<>();
private int totalSlots;
private StartupConf conf;
public TreeMap<Long, AtomicRangeGroup> nodes = new TreeMap<>();
public TreeMap<Long, String> groups = new TreeMap<>();
private int totalSlots;
private StartupConf conf;

public AtomicRangeGroup getGroupBykey(String key) {
return nodes.get(HashUtils.getHeadKey(this.nodes, key));
Expand All @@ -67,57 +67,10 @@ public TreeMap<Long, String> getGroups() {
return this.groups;
}

public void start() throws IOException {
PeerId serverId = new PeerId();
if (!serverId.parse(conf.getServerAddress())) {
throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
}

FileUtils.forceMkdir(new File(conf.getDataPath()));
// The same in-process raft group shares the same RPC Server.
RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
// Register biz handler
rpcServer.registerProcessor(new GetSlotsCommandProcessor(this));
rpcServer.registerProcessor(new GetCommandProcessor(this));
rpcServer.registerProcessor(new IncrementAndGetCommandProcessor(this));
rpcServer.registerProcessor(new CompareAndSetCommandProcessor(this));
rpcServer.registerProcessor(new SetCommandProcessor(this));

long step = conf.getMaxSlot() / totalSlots;
if (conf.getMaxSlot() % totalSlots > 0) {
step = step + 1;
}
for (int i = 0; i < totalSlots; i++) {
long min = i * step;
long mayMax = (i + 1) * step;
long max = mayMax > conf.getMaxSlot() || mayMax <= 0 ? conf.getMaxSlot() : mayMax;
StartupConf nodeConf = new StartupConf();
String nodeDataPath = conf.getDataPath() + File.separator + i;
nodeConf.setDataPath(nodeDataPath);
String nodeGroup = conf.getGroupId() + "_" + i;
nodeConf.setGroupId(nodeGroup);
nodeConf.setMaxSlot(max);
nodeConf.setMinSlot(min);
nodeConf.setConf(conf.getConf());
nodeConf.setServerAddress(conf.getServerAddress());
nodeConf.setTotalSlots(conf.getTotalSlots());
LOG.info("Starting range node {}-{} with conf {}", min, max, nodeConf);
nodes.put(i * step, AtomicRangeGroup.start(nodeConf, rpcServer));
groups.put(i * step, nodeGroup);
}
}

public static void start(String confFilePath) throws IOException {
StartupConf conf = new StartupConf();
if (!conf.loadFromFile(confFilePath)) {
throw new IllegalStateException("Load startup config from " + confFilePath + " failed");
}
AtomicServer server = new AtomicServer(conf);
server.start();
}

//for test

public static void main(String[] arsg) throws Exception {
start("config/server.properties");
StartupConf conf = new StartupConf();
conf.start("config/server.properties");
}
}
Loading
Loading