-
Notifications
You must be signed in to change notification settings - Fork 13
feature: add Sequencer id #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
examples/src/test/java/io/mosn/layotto/examples/sequencer/Sequencer.java
This file contains hidden or 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,38 @@ | ||
| package io.mosn.layotto.examples.sequencer; | ||
|
|
||
| import io.mosn.layotto.examples.file.File; | ||
| import io.mosn.layotto.v1.RuntimeClientBuilder; | ||
| import io.mosn.layotto.v1.config.RuntimeProperties; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import spec.sdk.runtime.v1.client.RuntimeClient; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdRequest; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdResponse; | ||
| import spec.sdk.runtime.v1.domain.sequencer.SequencerOptions; | ||
|
|
||
| public class Sequencer { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(Sequencer.class.getName()); | ||
|
|
||
| static String storeName = "redis"; | ||
| static String key = "examples"; | ||
| static SequencerOptions options = new SequencerOptions(); | ||
|
|
||
| public static void main(String[] args) { | ||
| options.setOption(SequencerOptions.AutoIncrement.WEAK); | ||
|
|
||
| RuntimeClient client = new RuntimeClientBuilder() | ||
| .withPort(RuntimeProperties.DEFAULT_PORT) | ||
| .build(); | ||
|
|
||
| GetNextIdRequest getNextIdRequest = new GetNextIdRequest(); | ||
| getNextIdRequest.setStoreName(storeName); | ||
| getNextIdRequest.setKey(key); | ||
| getNextIdRequest.setOptions(options); | ||
|
|
||
| GetNextIdResponse nextId = client.getNextId(getNextIdRequest); | ||
|
|
||
| System.out.println("id :"+nextId.getNextId()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
sdk/src/main/java/spec/sdk/runtime/v1/domain/sequencer/GetNextIdRequest.java
This file contains hidden or 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,59 @@ | ||
| package spec.sdk.runtime.v1.domain.sequencer; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class GetNextIdRequest { | ||
|
|
||
| private String storeName; | ||
|
|
||
| private String key; | ||
|
|
||
| private SequencerOptions options; | ||
|
|
||
LXPWing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private Map<String, String> metaData; | ||
|
|
||
| public String getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public void setKey(String key) { | ||
| this.key = key; | ||
| } | ||
|
|
||
| public SequencerOptions getOptions() { | ||
| return options; | ||
| } | ||
|
|
||
| public void setOptions(SequencerOptions options) { | ||
| this.options = options; | ||
| } | ||
|
|
||
| public String getStoreName() { | ||
| return storeName; | ||
| } | ||
|
|
||
| public void setStoreName(String storeName) { | ||
| this.storeName = storeName; | ||
| } | ||
|
|
||
| public Map<String, String> getMetaData() { | ||
| if (metaData == null) { | ||
| metaData = new HashMap<>(); | ||
| } | ||
|
|
||
| return metaData; | ||
| } | ||
|
|
||
| public void setMetadata(Map<String, String> metaData) { | ||
| this.metaData = metaData; | ||
| } | ||
|
|
||
| public Integer getOptionsValue() { | ||
| if(options != null && options.getOption() != null){ | ||
| return options.getOption().getValue(); | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
sdk/src/main/java/spec/sdk/runtime/v1/domain/sequencer/GetNextIdResponse.java
This file contains hidden or 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,14 @@ | ||
| package spec.sdk.runtime.v1.domain.sequencer; | ||
|
|
||
| public class GetNextIdResponse { | ||
|
|
||
| private long nextId; | ||
|
|
||
| public long getNextId() { | ||
| return nextId; | ||
| } | ||
|
|
||
| public void setNextId(long nextId) { | ||
| this.nextId = nextId; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
sdk/src/main/java/spec/sdk/runtime/v1/domain/sequencer/SequencerOptions.java
This file contains hidden or 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,28 @@ | ||
| package spec.sdk.runtime.v1.domain.sequencer; | ||
|
|
||
| public class SequencerOptions { | ||
|
|
||
| private AutoIncrement option; | ||
|
|
||
| public AutoIncrement getOption() { | ||
| return option; | ||
| } | ||
|
|
||
| public enum AutoIncrement { | ||
| WEAK(0), | ||
| STRONG(1); | ||
| private final Integer value; | ||
|
|
||
| AutoIncrement(Integer value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public Integer getValue() { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| public void setOption(AutoIncrement option) { | ||
| this.option = option; | ||
| } | ||
| } |
This file contains hidden or 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,69 @@ | ||
| package io.mosn.layotto.v1; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
| import io.grpc.stub.StreamObserver; | ||
| import io.grpc.testing.GrpcCleanupRule; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
| import spec.proto.runtime.v1.RuntimeGrpc; | ||
| import spec.proto.runtime.v1.RuntimeProto; | ||
| import spec.sdk.runtime.v1.client.RuntimeClient; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdRequest; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdResponse; | ||
| import spec.sdk.runtime.v1.domain.sequencer.SequencerOptions; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.AdditionalAnswers.delegatesTo; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class SequencerTest { | ||
| @Rule | ||
| public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); | ||
|
|
||
| private final RuntimeGrpc.RuntimeImplBase serviceImpl = mock(RuntimeGrpc.RuntimeImplBase.class,delegatesTo(new RuntimeGrpc.RuntimeImplBase(){ | ||
| @Override | ||
| public void getNextId(RuntimeProto.GetNextIdRequest request, StreamObserver<RuntimeProto.GetNextIdResponse> responseObserver) { | ||
| responseObserver.onNext( | ||
| RuntimeProto.GetNextIdResponse | ||
| .newBuilder() | ||
| .setNextId(1) | ||
| .build()); | ||
| responseObserver.onCompleted(); | ||
| } | ||
| })); | ||
|
|
||
| private RuntimeClient client; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| String serverName = InProcessServerBuilder.generateName(); | ||
|
|
||
| grpcCleanup.register(InProcessServerBuilder | ||
| .forName(serverName).directExecutor().addService(serviceImpl).build().start()); | ||
|
|
||
| ManagedChannel channel = grpcCleanup.register( | ||
| InProcessChannelBuilder.forName(serverName).directExecutor().build()); | ||
|
|
||
| client = new RuntimeClientBuilder() | ||
| .buildGrpcWithExistingChannel(channel); | ||
| } | ||
|
|
||
| @Test | ||
| public void getNextId() { | ||
| GetNextIdRequest getNextIdRequest = new GetNextIdRequest(); | ||
| SequencerOptions sequencerOptions = new SequencerOptions(); | ||
| sequencerOptions.setOption(SequencerOptions.AutoIncrement.WEAK); | ||
| getNextIdRequest.setStoreName("redis"); | ||
| getNextIdRequest.setKey("test"); | ||
| getNextIdRequest.setOptions(sequencerOptions); | ||
|
|
||
| GetNextIdResponse nextId = client.getNextId(getNextIdRequest); | ||
| assertEquals(nextId.getNextId(), 1); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
sdk/src/test/java/io/mosn/layotto/v1/SequencerTestWithRealServer.java
This file contains hidden or 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,68 @@ | ||
| package io.mosn.layotto.v1; | ||
|
|
||
| import io.grpc.Server; | ||
| import io.grpc.ServerBuilder; | ||
| import io.mosn.layotto.v1.grpc.ExceptionHandler; | ||
| import io.mosn.layotto.v1.grpc.GrpcRuntimeClient; | ||
| import io.mosn.layotto.v1.mock.MyFileService; | ||
| import io.mosn.layotto.v1.mock.MySequencerService; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
| import spec.proto.runtime.v1.RuntimeGrpc; | ||
| import spec.proto.runtime.v1.RuntimeProto; | ||
| import spec.sdk.runtime.v1.domain.sequencer.*; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class SequencerTestWithRealServer { | ||
|
|
||
| private final RuntimeGrpc.RuntimeImplBase sequencerService = new MySequencerService(); | ||
|
|
||
| private Server srv; | ||
| private GrpcRuntimeClient client; | ||
|
|
||
| int port = 9999; | ||
| String ip = "127.0.0.1"; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| // start grpc server | ||
| /* The port on which the server should run */ | ||
| srv = ServerBuilder.forPort(port) | ||
| .addService(sequencerService) | ||
| .intercept(new ExceptionHandler()) | ||
| .build() | ||
| .start(); | ||
|
|
||
| // build a client | ||
| client = new RuntimeClientBuilder() | ||
| .withIp(ip) | ||
| .withPort(port) | ||
| .withConnectionPoolSize(4) | ||
| .withTimeout(1000) | ||
| .buildGrpc(); | ||
| } | ||
|
|
||
| @After | ||
| public void shutdown() throws InterruptedException { | ||
| client.shutdown(); | ||
| srv.shutdownNow(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getNextId() { | ||
| GetNextIdRequest getNextIdRequest = new GetNextIdRequest(); | ||
| SequencerOptions sequencerOptions = new SequencerOptions(); | ||
| sequencerOptions.setOption(SequencerOptions.AutoIncrement.WEAK); | ||
| getNextIdRequest.setStoreName("redis"); | ||
| getNextIdRequest.setKey("test"); | ||
| getNextIdRequest.setOptions(sequencerOptions); | ||
|
|
||
| GetNextIdResponse nextId = client.getNextId(getNextIdRequest); | ||
| assertEquals(nextId.getNextId(), 1); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
sdk/src/test/java/io/mosn/layotto/v1/mock/MySequencerService.java
This file contains hidden or 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,17 @@ | ||
| package io.mosn.layotto.v1.mock; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
| import spec.proto.runtime.v1.RuntimeGrpc; | ||
| import spec.proto.runtime.v1.RuntimeProto; | ||
|
|
||
| public class MySequencerService extends RuntimeGrpc.RuntimeImplBase{ | ||
|
|
||
| @Override | ||
| public void getNextId(RuntimeProto.GetNextIdRequest request, StreamObserver<RuntimeProto.GetNextIdResponse> responseObserver) { | ||
| RuntimeProto.GetNextIdResponse response = RuntimeProto.GetNextIdResponse.newBuilder() | ||
| .setNextId(1) | ||
| .build(); | ||
| responseObserver.onNext(response); | ||
| responseObserver.onCompleted(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.