Repository: incubator-ratis Updated Branches: refs/heads/master 9774c5cb1 -> 48d6a2a42
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/48d6a2a4/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java ---------------------------------------------------------------------- diff --git a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java new file mode 100644 index 0000000..4a7a433 --- /dev/null +++ b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java @@ -0,0 +1,290 @@ +/** + * 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 org.apache.ratis.logservice.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.ratis.logservice.api.LogName; +import org.apache.ratis.logservice.api.LogStream; +import org.apache.ratis.proto.logservice.LogServiceProtos.AppendLogEntryReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.AppendLogEntryRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.ArchiveLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.ArchiveLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.CloseLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.CloseLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.CreateLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.CreateLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.DeleteLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.DeleteLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogLengthReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogLengthRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogStartIndexReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetLogStartIndexRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetStateReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.GetStateRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.ListLogsRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.LogServiceRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.ReadLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.ReadLogRequestProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.SyncLogReplyProto; +import org.apache.ratis.proto.logservice.LogServiceProtos.SyncLogRequestProto; +import org.junit.Ignore; +import org.junit.Test; + +public class TestLogServiceProtoUtil { + + + @Test + public void testAppendRequest() { + LogName name = LogName.of("test"); + List<byte[]> entries = new ArrayList<byte[]>(); + byte[] e1 = new byte[] {1,1}; + byte[] e2 = new byte[] {2,2}; + entries.add(e1); + entries.add(e2); + + LogServiceRequestProto proto = LogServiceProtoUtil.toAppendEntryLogRequestProto(name, entries); + AppendLogEntryRequestProto request = proto.getAppendRequest(); + assertEquals(name.getName(), request.getLogName().getName()); + assertEquals(2, request.getDataCount()); + assertTrue(TestUtils.equals(e1, request.getData(0).toByteArray())); + assertTrue(TestUtils.equals(e2, request.getData(1).toByteArray())); + + } + + @Test + public void testAppendReply() { + List<byte[]> entries = new ArrayList<byte[]>(); + byte[] e1 = new byte[] {1,1}; + byte[] e2 = new byte[] {2,2}; + entries.add(e1); + entries.add(e2); + + AppendLogEntryReplyProto proto = + LogServiceProtoUtil.toAppendLogReplyProto(null, null); + //TODO finish test + } + + @Test + public void testReadRequest() { + LogName name = LogName.of("test"); + + long start = 100; + int total = 5; + LogServiceRequestProto proto = LogServiceProtoUtil.toReadLogRequestProto(name, start, total); + ReadLogRequestProto request = proto.getReadNextQuery(); + assertEquals(name.getName(), request.getLogName().getName()); + assertEquals(100, request.getStartRecordId()); + assertEquals(5, request.getNumRecords()); + + } + + @Test + public void testReadReply() { + List<byte[]> entries = new ArrayList<byte[]>(); + byte[] e1 = new byte[] {1,1}; + byte[] e2 = new byte[] {2,2}; + entries.add(e1); + entries.add(e2); + + ReadLogReplyProto proto = + LogServiceProtoUtil.toReadLogReplyProto(entries, null); + + assertEquals(2, proto.getLogRecordCount()); + assertTrue(TestUtils.equals(e1, proto.getLogRecord(0).toByteArray())); + assertTrue(TestUtils.equals(e2, proto.getLogRecord(1).toByteArray())); + } + + @Test + public void testGetLengthReply() { + + long len = 100; + GetLogLengthReplyProto proto = + LogServiceProtoUtil.toGetLogLengthReplyProto(len, null); + assertEquals(len, proto.getLength()); + } + + @Test + public void testGetLengthRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toGetLengthRequestProto(name); + GetLogLengthRequestProto request = proto.getLengthQuery(); + assertEquals(name.getName(), request.getLogName().getName()); + } + + @Test + public void testGetStartIndexRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toGetStartIndexProto(name); + GetLogStartIndexRequestProto request = proto.getStartIndexQuery(); + assertEquals(name.getName(), request.getLogName().getName()); + } + + @Test + public void testGetStartIndexReply() { + + long index = 100; + GetLogStartIndexReplyProto proto = + LogServiceProtoUtil.toGetLogStartIndexReplyProto(index, null); + assertEquals(index, proto.getStartIndex()); + } + + @Test + public void testSyncRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toSyncLogRequestProto(name); + SyncLogRequestProto request = proto.getSyncRequest(); + assertEquals(name.getName(), request.getLogName().getName()); + } + + @Test + public void testSyncReply() { + + SyncLogReplyProto proto = + LogServiceProtoUtil.toSyncLogReplyProto(null); + //TODO finish test + } + + + //LIST LOGS + @Test + public void testListLogsRequest() { + LogServiceRequestProto proto = LogServiceProtoUtil.toListLogRequestProto(); + ListLogsRequestProto request = proto.getListLogs(); + //TODO finish + } + + @Test + public void testListLogsReply() { + + //TODO finish test + } + + //GET LOG + + @Test + public void testGetLogRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toGetLogRequestProto(name); + GetLogRequestProto request = proto.getGetLog(); + assertEquals(name.getName(), request.getLogName().getName()); + } + + @Test + @Ignore + public void testGetLogReply() { + LogStream logStream = null; + GetLogReplyProto proto = LogServiceProtoUtil.toGetLogReplyProto(logStream); + //TODO finish + + } + + //GET STATE + @Test + public void testGetStateRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toGetStateRequestProto(name); + GetStateRequestProto request = proto.getGetState(); + assertEquals(name.getName(), request.getLogName().getName()); + //TODO finish + } + + @Test + @Ignore + public void testGetStateReply() { + LogStream logStream = null; + GetStateReplyProto proto = LogServiceProtoUtil.toGetStateReplyProto(true); + //TODO finish + + } + //CREATE LOG + @Test + public void testCreateLogRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toCreateLogRequestProto(name); + CreateLogRequestProto request = proto.getCreateLog(); + assertEquals(name.getName(), request.getLogName().getName()); + //TODO finish + } + + @Test + @Ignore + public void testCreateLogReply() { + LogStream logStream = null; + CreateLogReplyProto proto = LogServiceProtoUtil.toCreateLogReplyProto(logStream); + //TODO finish + + } + //ARCHIVE LOG + @Test + public void testArchiveLogRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toArchiveLogRequestProto(name); + ArchiveLogRequestProto request = proto.getArchiveLog(); + assertEquals(name.getName(), request.getLogName().getName()); + //TODO finish + } + + @Test + @Ignore + public void testArchiveLogReply() { + ArchiveLogReplyProto proto = LogServiceProtoUtil.toArchiveLogReplyProto(); + //TODO finish + + } + //DELETE LOG + @Test + public void testDeleteLogRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toDeleteLogRequestProto(name); + DeleteLogRequestProto request = proto.getDeleteLog(); + assertEquals(name.getName(), request.getLogName().getName()); + //TODO finish + } + + @Test + @Ignore + public void testDeleteLogReply() { + DeleteLogReplyProto proto = LogServiceProtoUtil.toDeleteLogReplyProto(); + //TODO finish + + } + //CLOSE LOG + @Test + public void testCloseLogRequest() { + LogName name = LogName.of("test"); + LogServiceRequestProto proto = LogServiceProtoUtil.toCloseLogRequestProto(name); + CloseLogRequestProto request = proto.getCloseLog(); + assertEquals(name.getName(), request.getLogName().getName()); + //TODO finish + } + + @Test + @Ignore + public void testCloseLogReply() { + CloseLogReplyProto proto = LogServiceProtoUtil.toCloseLogReplyProto(); + //TODO finish + + } +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/48d6a2a4/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestUtils.java ---------------------------------------------------------------------- diff --git a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestUtils.java b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestUtils.java index 752889a..0b6a0e0 100644 --- a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestUtils.java +++ b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestUtils.java @@ -17,79 +17,21 @@ */ package org.apache.ratis.logservice.util; -import static org.apache.ratis.logservice.util.Utils.bytes2int; -import static org.apache.ratis.logservice.util.Utils.int2bytes; -import static org.apache.ratis.logservice.util.Utils.bytes2long; -import static org.apache.ratis.logservice.util.Utils.long2bytes; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - public class TestUtils { - @Test - public void testInt2Bytes() { - - byte[] buf = new byte[4]; - int v1 = 0; - int2bytes(v1, buf, 0); - int vv1 = bytes2int(buf, 0); - assertEquals(v1, vv1); - int v2 = 1; - int2bytes(v2, buf, 0); - int vv2 = bytes2int(buf, 0); - assertEquals(v2, vv2); - int v3 = -1; - int2bytes(v3, buf, 0); - int vv3 = bytes2int(buf, 0); - assertEquals(v3, vv3); - int v4 = Integer.MIN_VALUE; - int2bytes(v4, buf, 0); - int vv4 = bytes2int(buf, 0); - assertEquals(v4, vv4); - int v5 = Integer.MAX_VALUE; - int2bytes(v5, buf, 0); - int vv5 = bytes2int(buf, 0); - assertEquals(v5, vv5); - - } - - - @Test - public void testLong2Bytes() { - byte[] buf = new byte[8]; - long v1 = 0; - long2bytes(v1, buf, 0); - long vv1 = bytes2long(buf, 0); - assertEquals(v1, vv1); - - long v2 = 1; - long2bytes(v2, buf, 0); - long vv2 = bytes2long(buf, 0); - assertEquals(v2, vv2); - - long v3 = -1; - long2bytes(v3, buf, 0); - long vv3 = bytes2long(buf, 0); - assertEquals(v3, vv3); - - long v4 = Long.MIN_VALUE; - long2bytes(v4, buf, 0); - long vv4 = bytes2long(buf, 0); - assertEquals(v4, vv4); - - long v5 = Integer.MAX_VALUE; - long2bytes(v5, buf, 0); - long vv5 = bytes2long(buf, 0); - assertEquals(v5, vv5); - - long v6 = 100; - buf = new byte[20]; - long2bytes(v6, buf, 12); - long vv6 = bytes2long(buf, 12); - assertEquals(v6, vv6); + public static boolean equals(byte[] a, byte[] b) { + if (a == null || b == null) { + return false; + } + if (a.length != b.length) { + return false; + } + for(int i=0; i < a.length; i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; } - - } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/48d6a2a4/ratis-proto/src/main/proto/Logservice.proto ---------------------------------------------------------------------- diff --git a/ratis-proto/src/main/proto/Logservice.proto b/ratis-proto/src/main/proto/Logservice.proto index 40964e0..b89ed49 100644 --- a/ratis-proto/src/main/proto/Logservice.proto +++ b/ratis-proto/src/main/proto/Logservice.proto @@ -21,31 +21,94 @@ option java_outer_classname = "LogServiceProtos"; option java_generate_equals_and_hash = true; package ratis.logservice; -enum MessageType { - READ_REQUEST = 0; - READ_REPLY = 1; - WRITE = 2; + +// Generic message for Log Service exception +message LogServiceException { + string exceptionClassName = 1; + string errorMsg = 2; + bytes stacktrace = 3; +} + +// Write request (append log entry(ies)) +message AppendLogEntryRequestProto { + LogNameProto logName = 1; + repeated bytes data = 2; +} + +// Write reply +message AppendLogEntryReplyProto { + repeated uint64 recordId = 1; + // optional + LogServiceException exception = 2; +} + + +// Sync log (make all previous writes durable) +message SyncLogRequestProto { + LogNameProto logName = 1; +} + +// Sync reply +message SyncLogReplyProto { + // optional + LogServiceException exception = 1; +} + +// Read request +message ReadLogRequestProto { + LogNameProto logName = 1; + uint32 numRecords = 2; + // start record id + uint64 startRecordId = 3; +} + +// Read reply +message ReadLogReplyProto { + repeated bytes logRecord = 1; + // optional + LogServiceException exception = 2; } -message LogMessage { - MessageType type = 1; - string log_name = 2; - uint64 length = 3; - bytes data = 4; +// Get log length request +message GetLogLengthRequestProto { + LogNameProto logName = 1; } + +// Get log length reply +message GetLogLengthReplyProto { + uint64 length = 1; + //optional + LogServiceException exception = 2; +} + +message GetLogStartIndexRequestProto { + LogNameProto logName = 1; +} + +message GetLogStartIndexReplyProto { + uint64 startIndex = 1; + //optional + LogServiceException exception = 2; +} + message LogServiceRequestProto { oneof Request { - LogMessage logMessage = 1; - CreateLogRequestProto createLog = 2; - ListLogsRequestProto listLogs = 3; - GetLogRequestProto getLog = 4; - CloseLogRequestProto closeLog = 5; - GetStateRequestProto getState = 6; - ArchiveLogRequestProto archiveLog = 7; - DeleteLogRequestProto deleteLog = 8; + CreateLogRequestProto createLog = 1; + ListLogsRequestProto listLogs = 2; + GetLogRequestProto getLog = 3; + CloseLogRequestProto closeLog = 4; + GetStateRequestProto getState = 5; + ArchiveLogRequestProto archiveLog = 6; + DeleteLogRequestProto deleteLog = 7; + ReadLogRequestProto readNextQuery = 8; + GetLogLengthRequestProto lengthQuery = 9; + GetLogStartIndexRequestProto startIndexQuery = 10; + AppendLogEntryRequestProto appendRequest = 11; + SyncLogRequestProto syncRequest = 12; } } + message LogNameProto { string name = 1; } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/48d6a2a4/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java index 4622002..53b77a3 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java @@ -256,7 +256,7 @@ public class RaftServerProxy implements RaftServer { return getImpl(ProtoUtils.toRaftGroupId(proto.getRaftGroupId())); } - RaftServerImpl getImpl(RaftGroupId groupId) throws IOException { + public RaftServerImpl getImpl(RaftGroupId groupId) throws IOException { Objects.requireNonNull(groupId, "groupId == null"); return IOUtils.getFromFuture(getImplFuture(groupId), getId()); }
