forked from sofastack/sofa-jraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) counter example (sofastack#318)
* (feat) counter example * Add final to fields * (feat) catch exception in GetValueRequestProcessor * (feat)async GetValueRequestProcessor
- Loading branch information
1 parent
4016336
commit 4b2bc6d
Showing
10 changed files
with
361 additions
and
140 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterClosure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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; | ||
|
||
import com.alipay.sofa.jraft.Closure; | ||
import com.alipay.sofa.jraft.example.counter.rpc.ValueResponse; | ||
|
||
/** | ||
* @author likun ([email protected]) | ||
*/ | ||
public abstract class CounterClosure implements Closure { | ||
|
||
private ValueResponse valueResponse; | ||
private CounterOperation counterOperation; | ||
|
||
public void setCounterOperation(CounterOperation counterOperation) { | ||
this.counterOperation = counterOperation; | ||
} | ||
|
||
public CounterOperation getCounterOperation() { | ||
return counterOperation; | ||
} | ||
|
||
public ValueResponse getValueResponse() { | ||
return valueResponse; | ||
} | ||
|
||
public void setValueResponse(ValueResponse valueResponse) { | ||
this.valueResponse = valueResponse; | ||
} | ||
|
||
protected void failure(final String errorMsg, final String redirect) { | ||
final ValueResponse response = new ValueResponse(); | ||
response.setSuccess(false); | ||
response.setErrorMsg(errorMsg); | ||
response.setRedirect(redirect); | ||
setValueResponse(response); | ||
} | ||
|
||
protected void success(final long value) { | ||
final ValueResponse response = new ValueResponse(); | ||
response.setValue(value); | ||
response.setSuccess(true); | ||
setValueResponse(response); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* The counter operation | ||
* | ||
* @author likun ([email protected]) | ||
*/ | ||
public class CounterOperation implements Serializable { | ||
|
||
private static final long serialVersionUID = -6597003954824547294L; | ||
|
||
/** Get value */ | ||
public static final byte GET = 0x01; | ||
/** Increment and get value */ | ||
public static final byte INCREMENT = 0x02; | ||
|
||
private byte op; | ||
private long delta; | ||
|
||
public static CounterOperation createGet() { | ||
return new CounterOperation(GET); | ||
} | ||
|
||
public static CounterOperation createIncrement(final long delta) { | ||
return new CounterOperation(INCREMENT, delta); | ||
} | ||
|
||
public CounterOperation(byte op) { | ||
this(op, 0); | ||
} | ||
|
||
public CounterOperation(byte op, long delta) { | ||
this.op = op; | ||
this.delta = delta; | ||
} | ||
|
||
public byte getOp() { | ||
return op; | ||
} | ||
|
||
public long getDelta() { | ||
return delta; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* The counter service supporting query and count function. | ||
* | ||
* @author likun ([email protected]) | ||
*/ | ||
public interface CounterService { | ||
|
||
/** | ||
* Get current value from counter | ||
* | ||
* Provide consistent reading if {@code readOnlySafe} is true. | ||
*/ | ||
void get(final boolean readOnlySafe, final CounterClosure closure); | ||
|
||
/** | ||
* Add delta to counter then get value | ||
*/ | ||
void incrementAndGet(final long delta, final CounterClosure closure); | ||
} |
124 changes: 124 additions & 0 deletions
124
jraft-example/src/main/java/com/alipay/sofa/jraft/example/counter/CounterServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.Executor; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.alipay.remoting.exception.CodecException; | ||
import com.alipay.remoting.serialization.SerializerManager; | ||
import com.alipay.sofa.jraft.Status; | ||
import com.alipay.sofa.jraft.closure.ReadIndexClosure; | ||
import com.alipay.sofa.jraft.entity.Task; | ||
import com.alipay.sofa.jraft.error.RaftError; | ||
import com.alipay.sofa.jraft.rhea.StoreEngineHelper; | ||
import com.alipay.sofa.jraft.rhea.options.StoreEngineOptions; | ||
import com.alipay.sofa.jraft.util.BytesUtil; | ||
|
||
/** | ||
* @author likun ([email protected]) | ||
*/ | ||
public class CounterServiceImpl implements CounterService { | ||
private static final Logger LOG = LoggerFactory.getLogger(CounterServiceImpl.class); | ||
|
||
private final CounterServer counterServer; | ||
private final Executor readIndexExecutor; | ||
|
||
public CounterServiceImpl(CounterServer counterServer) { | ||
this.counterServer = counterServer; | ||
this.readIndexExecutor = createReadIndexExecutor(); | ||
} | ||
|
||
private Executor createReadIndexExecutor() { | ||
final StoreEngineOptions opts = new StoreEngineOptions(); | ||
return StoreEngineHelper.createReadIndexExecutor(opts.getReadIndexCoreThreads()); | ||
} | ||
|
||
@Override | ||
public void get(final boolean readOnlySafe, final CounterClosure closure) { | ||
if(!readOnlySafe){ | ||
closure.success(getValue()); | ||
closure.run(Status.OK()); | ||
return; | ||
} | ||
|
||
this.counterServer.getNode().readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() { | ||
@Override | ||
public void run(Status status, long index, byte[] reqCtx) { | ||
if(status.isOk()){ | ||
closure.success(getValue()); | ||
closure.run(Status.OK()); | ||
return; | ||
} | ||
CounterServiceImpl.this.readIndexExecutor.execute(() -> { | ||
if(isLeader()){ | ||
LOG.debug("Fail to get value with 'ReadIndex': {}, try to applying to the state machine.", status); | ||
applyOperation(CounterOperation.createGet(), closure); | ||
}else { | ||
handlerNotLeaderError(closure); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
private boolean isLeader() { | ||
return this.counterServer.getFsm().isLeader(); | ||
} | ||
|
||
private long getValue() { | ||
return this.counterServer.getFsm().getValue(); | ||
} | ||
|
||
private String getRedirect() { | ||
return this.counterServer.redirect().getRedirect(); | ||
} | ||
|
||
@Override | ||
public void incrementAndGet(final long delta, final CounterClosure closure) { | ||
applyOperation(CounterOperation.createIncrement(delta), closure); | ||
} | ||
|
||
private void applyOperation(final CounterOperation op, final CounterClosure closure) { | ||
if (!isLeader()) { | ||
handlerNotLeaderError(closure); | ||
return; | ||
} | ||
|
||
try { | ||
closure.setCounterOperation(op); | ||
final Task task = new Task(); | ||
task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op))); | ||
task.setDone(closure); | ||
this.counterServer.getNode().apply(task); | ||
} catch (CodecException e) { | ||
String errorMsg = "Fail to encode CounterOperation"; | ||
LOG.error(errorMsg, e); | ||
closure.failure(errorMsg, StringUtils.EMPTY); | ||
closure.run(new Status(RaftError.EINTERNAL, errorMsg)); | ||
} | ||
} | ||
|
||
private void handlerNotLeaderError(final CounterClosure closure) { | ||
closure.failure("Not leader.", getRedirect()); | ||
closure.run(new Status(RaftError.EPERM, "Not leader")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.