Skip to content

Commit 883e270

Browse files
committed
Refactor: Change bidirectional association to unidirectional association
CounterServer: class participates in a cyclic dependency. The participating classes in the cycle are: CounterServiceImpl; CounterServer
1 parent 2f8d90c commit 883e270

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterServer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public class CounterServer {
4646
private CounterStateMachine fsm;
4747

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

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

5960
// register business processor
60-
CounterService counterService = new CounterServiceImpl(this);
61+
CounterService counterService = counterServiceProvider.getCounterService();
6162
rpcServer.registerProcessor(new GetValueRequestProcessor(counterService));
6263
rpcServer.registerProcessor(new IncrementAndGetRequestProcessor(counterService));
6364
// init state machine
@@ -116,6 +117,8 @@ public static void main(final String[] args) throws IOException {
116117
final String serverIdStr = args[2];
117118
final String initConfStr = args[3];
118119

120+
CounterServiceProvider counterServiceProvider = new CounterServiceImpl(null);
121+
119122
final NodeOptions nodeOptions = new NodeOptions();
120123
// for test, modify some params
121124
// set election timeout to 1s
@@ -137,7 +140,9 @@ public static void main(final String[] args) throws IOException {
137140
nodeOptions.setInitialConf(initConf);
138141

139142
// start raft server
140-
final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions);
143+
final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions,
144+
counterServiceProvider);
145+
// ((CounterServiceImpl) counterServiceProvider).counterServer = counterServer;
141146
System.out.println("Started counter server at port:"
142147
+ counterServer.getNode().getNodeId().getPeerId().getPort());
143148
// GrpcServer need block to prevent process exit

jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* @author likun (saimu.msm@antfin.com)
3838
*/
39-
public class CounterServiceImpl implements CounterService {
39+
public class CounterServiceImpl implements CounterService, CounterServiceProvider {
4040
private static final Logger LOG = LoggerFactory.getLogger(CounterServiceImpl.class);
4141

4242
private final CounterServer counterServer;
@@ -47,6 +47,11 @@ public CounterServiceImpl(CounterServer counterServer) {
4747
this.readIndexExecutor = createReadIndexExecutor();
4848
}
4949

50+
@Override
51+
public CounterService getCounterService() {
52+
return this;
53+
}
54+
5055
private Executor createReadIndexExecutor() {
5156
final StoreEngineOptions opts = new StoreEngineOptions();
5257
return StoreEngineHelper.createReadIndexExecutor(opts.getReadIndexCoreThreads());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.jraft.example.counter;
18+
19+
public interface CounterServiceProvider {
20+
CounterService getCounterService();
21+
}

0 commit comments

Comments
 (0)