Yuta HIGUCHI

renamed OnosTimestamp -> MastershipBasedTimestamp

Change-Id: Ie6ee5d85bc0f5aadfa32e27e1d31e3c1d5c9fea8
1 -package org.onlab.onos.store.impl; 1 +package org.onlab.onos.store.common.impl;
2 2
3 import static com.google.common.base.Preconditions.checkArgument; 3 import static com.google.common.base.Preconditions.checkArgument;
4 4
...@@ -9,12 +9,11 @@ import org.onlab.onos.store.Timestamp; ...@@ -9,12 +9,11 @@ import org.onlab.onos.store.Timestamp;
9 import com.google.common.base.MoreObjects; 9 import com.google.common.base.MoreObjects;
10 import com.google.common.collect.ComparisonChain; 10 import com.google.common.collect.ComparisonChain;
11 11
12 -// If it is store specific, implement serializable interfaces?
13 /** 12 /**
14 * Default implementation of Timestamp. 13 * Default implementation of Timestamp.
15 * TODO: Better documentation. 14 * TODO: Better documentation.
16 */ 15 */
17 -public final class OnosTimestamp implements Timestamp { 16 +public final class MastershipBasedTimestamp implements Timestamp {
18 17
19 private final int termNumber; 18 private final int termNumber;
20 private final int sequenceNumber; 19 private final int sequenceNumber;
...@@ -25,15 +24,16 @@ public final class OnosTimestamp implements Timestamp { ...@@ -25,15 +24,16 @@ public final class OnosTimestamp implements Timestamp {
25 * @param termNumber the mastership termNumber 24 * @param termNumber the mastership termNumber
26 * @param sequenceNumber the sequenceNumber number within the termNumber 25 * @param sequenceNumber the sequenceNumber number within the termNumber
27 */ 26 */
28 - public OnosTimestamp(int termNumber, int sequenceNumber) { 27 + public MastershipBasedTimestamp(int termNumber, int sequenceNumber) {
29 this.termNumber = termNumber; 28 this.termNumber = termNumber;
30 this.sequenceNumber = sequenceNumber; 29 this.sequenceNumber = sequenceNumber;
31 } 30 }
32 31
33 @Override 32 @Override
34 public int compareTo(Timestamp o) { 33 public int compareTo(Timestamp o) {
35 - checkArgument(o instanceof OnosTimestamp, "Must be OnosTimestamp", o); 34 + checkArgument(o instanceof MastershipBasedTimestamp,
36 - OnosTimestamp that = (OnosTimestamp) o; 35 + "Must be MastershipBasedTimestamp", o);
36 + MastershipBasedTimestamp that = (MastershipBasedTimestamp) o;
37 37
38 return ComparisonChain.start() 38 return ComparisonChain.start()
39 .compare(this.termNumber, that.termNumber) 39 .compare(this.termNumber, that.termNumber)
...@@ -51,10 +51,10 @@ public final class OnosTimestamp implements Timestamp { ...@@ -51,10 +51,10 @@ public final class OnosTimestamp implements Timestamp {
51 if (this == obj) { 51 if (this == obj) {
52 return true; 52 return true;
53 } 53 }
54 - if (!(obj instanceof OnosTimestamp)) { 54 + if (!(obj instanceof MastershipBasedTimestamp)) {
55 return false; 55 return false;
56 } 56 }
57 - OnosTimestamp that = (OnosTimestamp) obj; 57 + MastershipBasedTimestamp that = (MastershipBasedTimestamp) obj;
58 return Objects.equals(this.termNumber, that.termNumber) && 58 return Objects.equals(this.termNumber, that.termNumber) &&
59 Objects.equals(this.sequenceNumber, that.sequenceNumber); 59 Objects.equals(this.sequenceNumber, that.sequenceNumber);
60 } 60 }
...@@ -87,7 +87,7 @@ public final class OnosTimestamp implements Timestamp { ...@@ -87,7 +87,7 @@ public final class OnosTimestamp implements Timestamp {
87 87
88 // Default constructor for serialization 88 // Default constructor for serialization
89 @Deprecated 89 @Deprecated
90 - protected OnosTimestamp() { 90 + protected MastershipBasedTimestamp() {
91 this.termNumber = -1; 91 this.termNumber = -1;
92 this.sequenceNumber = -1; 92 this.sequenceNumber = -1;
93 } 93 }
......
...@@ -15,7 +15,7 @@ import org.onlab.onos.net.DeviceId; ...@@ -15,7 +15,7 @@ import org.onlab.onos.net.DeviceId;
15 import org.onlab.onos.store.ClockProviderService; 15 import org.onlab.onos.store.ClockProviderService;
16 import org.onlab.onos.store.ClockService; 16 import org.onlab.onos.store.ClockService;
17 import org.onlab.onos.store.Timestamp; 17 import org.onlab.onos.store.Timestamp;
18 -import org.onlab.onos.store.impl.OnosTimestamp; 18 +import org.onlab.onos.store.common.impl.MastershipBasedTimestamp;
19 import org.slf4j.Logger; 19 import org.slf4j.Logger;
20 20
21 /** 21 /**
...@@ -47,7 +47,7 @@ public class DeviceClockManager implements ClockService, ClockProviderService { ...@@ -47,7 +47,7 @@ public class DeviceClockManager implements ClockService, ClockProviderService {
47 if (term == null) { 47 if (term == null) {
48 throw new IllegalStateException("Requesting timestamp for a deviceId without mastership"); 48 throw new IllegalStateException("Requesting timestamp for a deviceId without mastership");
49 } 49 }
50 - return new OnosTimestamp(term.termNumber(), ticker.incrementAndGet()); 50 + return new MastershipBasedTimestamp(term.termNumber(), ticker.incrementAndGet());
51 } 51 }
52 52
53 @Override 53 @Override
......
1 package org.onlab.onos.store.serializers; 1 package org.onlab.onos.store.serializers;
2 2
3 -import org.onlab.onos.store.impl.OnosTimestamp; 3 +import org.onlab.onos.store.common.impl.MastershipBasedTimestamp;
4 4
5 import com.esotericsoftware.kryo.Kryo; 5 import com.esotericsoftware.kryo.Kryo;
6 import com.esotericsoftware.kryo.Serializer; 6 import com.esotericsoftware.kryo.Serializer;
7 import com.esotericsoftware.kryo.io.Input; 7 import com.esotericsoftware.kryo.io.Input;
8 import com.esotericsoftware.kryo.io.Output; 8 import com.esotericsoftware.kryo.io.Output;
9 9
10 +// To be used if Timestamp ever needs to cross bundle boundary.
10 /** 11 /**
11 - * Kryo Serializer for {@link OnosTimestamp}. 12 + * Kryo Serializer for {@link MastershipBasedTimestamp}.
12 */ 13 */
13 -public class OnosTimestampSerializer extends Serializer<OnosTimestamp> { 14 +public class MastershipBasedTimestampSerializer extends Serializer<MastershipBasedTimestamp> {
14 15
15 /** 16 /**
16 * Default constructor. 17 * Default constructor.
17 */ 18 */
18 - public OnosTimestampSerializer() { 19 + public MastershipBasedTimestampSerializer() {
19 // non-null, immutable 20 // non-null, immutable
20 super(false, true); 21 super(false, true);
21 } 22 }
22 23
23 @Override 24 @Override
24 - public void write(Kryo kryo, Output output, OnosTimestamp object) { 25 + public void write(Kryo kryo, Output output, MastershipBasedTimestamp object) {
25 output.writeInt(object.termNumber()); 26 output.writeInt(object.termNumber());
26 output.writeInt(object.sequenceNumber()); 27 output.writeInt(object.sequenceNumber());
27 } 28 }
28 29
29 @Override 30 @Override
30 - public OnosTimestamp read(Kryo kryo, Input input, Class<OnosTimestamp> type) { 31 + public MastershipBasedTimestamp read(Kryo kryo, Input input, Class<MastershipBasedTimestamp> type) {
31 final int term = input.readInt(); 32 final int term = input.readInt();
32 final int sequence = input.readInt(); 33 final int sequence = input.readInt();
33 - return new OnosTimestamp(term, sequence); 34 + return new MastershipBasedTimestamp(term, sequence);
34 } 35 }
35 } 36 }
......
1 +package org.onlab.onos.store.common.impl;
2 +
3 +import static org.junit.Assert.*;
4 +
5 +import java.nio.ByteBuffer;
6 +
7 +import org.junit.Test;
8 +import org.onlab.onos.store.Timestamp;
9 +import org.onlab.onos.store.serializers.MastershipBasedTimestampSerializer;
10 +import org.onlab.util.KryoPool;
11 +
12 +import com.google.common.testing.EqualsTester;
13 +
14 +/**
15 + * Test of {@link MastershipBasedTimestamp}.
16 + */
17 +public class MastershipBasedTimestampTest {
18 +
19 + private static final Timestamp TS_1_1 = new MastershipBasedTimestamp(1, 1);
20 + private static final Timestamp TS_1_2 = new MastershipBasedTimestamp(1, 2);
21 + private static final Timestamp TS_2_1 = new MastershipBasedTimestamp(2, 1);
22 + private static final Timestamp TS_2_2 = new MastershipBasedTimestamp(2, 2);
23 +
24 + @Test
25 + public final void testBasic() {
26 + final int termNumber = 5;
27 + final int sequenceNumber = 6;
28 + MastershipBasedTimestamp ts = new MastershipBasedTimestamp(termNumber,
29 + sequenceNumber);
30 +
31 + assertEquals(termNumber, ts.termNumber());
32 + assertEquals(sequenceNumber, ts.sequenceNumber());
33 + }
34 +
35 + @Test
36 + public final void testCompareTo() {
37 + assertTrue(TS_1_1.compareTo(TS_1_1) == 0);
38 + assertTrue(TS_1_1.compareTo(new MastershipBasedTimestamp(1, 1)) == 0);
39 +
40 + assertTrue(TS_1_1.compareTo(TS_1_2) < 0);
41 + assertTrue(TS_1_2.compareTo(TS_1_1) > 0);
42 +
43 + assertTrue(TS_1_2.compareTo(TS_2_1) < 0);
44 + assertTrue(TS_1_2.compareTo(TS_2_2) < 0);
45 + assertTrue(TS_2_1.compareTo(TS_1_1) > 0);
46 + assertTrue(TS_2_2.compareTo(TS_1_1) > 0);
47 + }
48 +
49 + @Test
50 + public final void testEqualsObject() {
51 + new EqualsTester()
52 + .addEqualityGroup(new MastershipBasedTimestamp(1, 1),
53 + new MastershipBasedTimestamp(1, 1), TS_1_1)
54 + .addEqualityGroup(new MastershipBasedTimestamp(1, 2),
55 + new MastershipBasedTimestamp(1, 2), TS_1_2)
56 + .addEqualityGroup(new MastershipBasedTimestamp(2, 1),
57 + new MastershipBasedTimestamp(2, 1), TS_2_1)
58 + .addEqualityGroup(new MastershipBasedTimestamp(2, 2),
59 + new MastershipBasedTimestamp(2, 2), TS_2_2)
60 + .testEquals();
61 + }
62 +
63 + @Test
64 + public final void testKryoSerializable() {
65 + final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
66 + final KryoPool kryos = KryoPool.newBuilder()
67 + .register(MastershipBasedTimestamp.class)
68 + .build();
69 +
70 + kryos.serialize(TS_2_1, buffer);
71 + buffer.flip();
72 + Timestamp copy = kryos.deserialize(buffer);
73 +
74 + new EqualsTester()
75 + .addEqualityGroup(TS_2_1, copy)
76 + .testEquals();
77 + }
78 +
79 + @Test
80 + public final void testKryoSerializableWithHandcraftedSerializer() {
81 + final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
82 + final KryoPool kryos = KryoPool.newBuilder()
83 + .register(MastershipBasedTimestamp.class, new MastershipBasedTimestampSerializer())
84 + .build();
85 +
86 + kryos.serialize(TS_1_2, buffer);
87 + buffer.flip();
88 + Timestamp copy = kryos.deserialize(buffer);
89 +
90 + new EqualsTester()
91 + .addEqualityGroup(TS_1_2, copy)
92 + .testEquals();
93 + }
94 +
95 +}
...@@ -4,38 +4,20 @@ import static org.junit.Assert.*; ...@@ -4,38 +4,20 @@ import static org.junit.Assert.*;
4 4
5 import java.nio.ByteBuffer; 5 import java.nio.ByteBuffer;
6 6
7 -import org.junit.After;
8 -import org.junit.AfterClass;
9 -import org.junit.Before;
10 -import org.junit.BeforeClass;
11 import org.junit.Test; 7 import org.junit.Test;
12 import org.onlab.onos.store.Timestamp; 8 import org.onlab.onos.store.Timestamp;
13 -import org.onlab.onos.store.impl.OnosTimestamp;
14 import org.onlab.util.KryoPool; 9 import org.onlab.util.KryoPool;
15 10
16 import com.google.common.testing.EqualsTester; 11 import com.google.common.testing.EqualsTester;
17 12
13 +/**
14 + * Test of {@link Timestamped}.
15 + */
18 public class TimestampedTest { 16 public class TimestampedTest {
19 17
20 - private static final Timestamp TS_1_1 = new OnosTimestamp(1, 1); 18 + private static final Timestamp TS_1_1 = new MastershipBasedTimestamp(1, 1);
21 - private static final Timestamp TS_1_2 = new OnosTimestamp(1, 2); 19 + private static final Timestamp TS_1_2 = new MastershipBasedTimestamp(1, 2);
22 - private static final Timestamp TS_2_1 = new OnosTimestamp(2, 1); 20 + private static final Timestamp TS_2_1 = new MastershipBasedTimestamp(2, 1);
23 -
24 - @BeforeClass
25 - public static void setUpBeforeClass() throws Exception {
26 - }
27 -
28 - @AfterClass
29 - public static void tearDownAfterClass() throws Exception {
30 - }
31 -
32 - @Before
33 - public void setUp() throws Exception {
34 - }
35 -
36 - @After
37 - public void tearDown() throws Exception {
38 - }
39 21
40 @Test 22 @Test
41 public final void testHashCode() { 23 public final void testHashCode() {
...@@ -97,7 +79,7 @@ public class TimestampedTest { ...@@ -97,7 +79,7 @@ public class TimestampedTest {
97 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); 79 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
98 final KryoPool kryos = KryoPool.newBuilder() 80 final KryoPool kryos = KryoPool.newBuilder()
99 .register(Timestamped.class, 81 .register(Timestamped.class,
100 - OnosTimestamp.class) 82 + MastershipBasedTimestamp.class)
101 .build(); 83 .build();
102 84
103 Timestamped<String> original = new Timestamped<>("foobar", TS_1_1); 85 Timestamped<String> original = new Timestamped<>("foobar", TS_1_1);
......