-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IGNITE-23494 Do not use ByteUtils#toBytes to serialize assignments (#…
- Loading branch information
Showing
7 changed files
with
174 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
77 changes: 77 additions & 0 deletions
77
...src/main/java/org/apache/ignite/internal/partitiondistribution/AssignmentsSerializer.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,77 @@ | ||
/* | ||
* 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.ignite.internal.partitiondistribution; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.apache.ignite.internal.util.io.IgniteDataInput; | ||
import org.apache.ignite.internal.util.io.IgniteDataOutput; | ||
import org.apache.ignite.internal.versioned.VersionedSerializer; | ||
|
||
/** | ||
* {@link VersionedSerializer} for {@link Assignments} instances. | ||
*/ | ||
public class AssignmentsSerializer extends VersionedSerializer<Assignments> { | ||
/** Serializer instance. */ | ||
public static final AssignmentsSerializer INSTANCE = new AssignmentsSerializer(); | ||
|
||
@Override | ||
protected void writeExternalData(Assignments assignments, IgniteDataOutput out) throws IOException { | ||
out.writeVarInt(assignments.nodes().size()); | ||
for (Assignment assignment : assignments.nodes()) { | ||
writeAssignment(assignment, out); | ||
} | ||
|
||
out.writeBoolean(assignments.force()); | ||
// Writing long and not varlong as the latter will take 9 bytes for timestamps. | ||
out.writeLong(assignments.timestamp()); | ||
} | ||
|
||
private static void writeAssignment(Assignment assignment, IgniteDataOutput out) throws IOException { | ||
out.writeUTF(assignment.consistentId()); | ||
out.writeBoolean(assignment.isPeer()); | ||
} | ||
|
||
@Override | ||
protected Assignments readExternalData(byte protoVer, IgniteDataInput in) throws IOException { | ||
Set<Assignment> nodes = readNodes(in); | ||
boolean force = in.readBoolean(); | ||
long timestamp = in.readLong(); | ||
|
||
return force ? Assignments.forced(nodes, timestamp) : Assignments.of(nodes, timestamp); | ||
} | ||
|
||
private static Set<Assignment> readNodes(IgniteDataInput in) throws IOException { | ||
int length = in.readVarIntAsInt(); | ||
|
||
Set<Assignment> nodes = new HashSet<>(); | ||
for (int i = 0; i < length; i++) { | ||
nodes.add(readAssignment(in)); | ||
} | ||
|
||
return nodes; | ||
} | ||
|
||
private static Assignment readAssignment(IgniteDataInput in) throws IOException { | ||
String consistentId = in.readUTF(); | ||
boolean isPeer = in.readBoolean(); | ||
|
||
return isPeer ? Assignment.forPeer(consistentId) : Assignment.forLearner(consistentId); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...test/java/org/apache/ignite/internal/partitiondistribution/AssignmentsSerializerTest.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,89 @@ | ||
/* | ||
* 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.ignite.internal.partitiondistribution; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.ignite.internal.versioned.VersionedSerialization; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class AssignmentsSerializerTest { | ||
private static final String NOT_FORCED_ASSIGNMENTS_SERIALIZED_WITH_V1 = "Ae++QwMCYQECYgAA6AMAAAAAAAA="; | ||
private static final String FORCED_ASSIGNMENTS_SERIALIZED_WITH_V1 = "Ae++QwMCYQECYgAB6AMAAAAAAAA="; | ||
|
||
private final AssignmentsSerializer serializer = new AssignmentsSerializer(); | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {true, false}) | ||
void serializationAndDeserialization(boolean force) { | ||
Set<Assignment> nodes = Set.of(Assignment.forPeer("abc"), Assignment.forLearner("def")); | ||
Assignments originalAssignments = force ? Assignments.forced(nodes, 1000L) : Assignments.of(nodes, 1000L); | ||
|
||
byte[] bytes = VersionedSerialization.toBytes(originalAssignments, serializer); | ||
Assignments restoredAssignments = VersionedSerialization.fromBytes(bytes, serializer); | ||
|
||
assertThat(restoredAssignments, equalTo(originalAssignments)); | ||
} | ||
|
||
@Test | ||
void v1NotForcedCanBeDeserialized() { | ||
byte[] bytes = Base64.getDecoder().decode(NOT_FORCED_ASSIGNMENTS_SERIALIZED_WITH_V1); | ||
Assignments restoredAssignments = VersionedSerialization.fromBytes(bytes, serializer); | ||
|
||
assertNodesFromV1(restoredAssignments); | ||
|
||
assertThat(restoredAssignments.force(), is(false)); | ||
assertThat(restoredAssignments.timestamp(), is(1000L)); | ||
} | ||
|
||
@Test | ||
void v1ForcedCanBeDeserialized() { | ||
byte[] bytes = Base64.getDecoder().decode(FORCED_ASSIGNMENTS_SERIALIZED_WITH_V1); | ||
Assignments restoredAssignments = VersionedSerialization.fromBytes(bytes, serializer); | ||
|
||
assertNodesFromV1(restoredAssignments); | ||
|
||
assertThat(restoredAssignments.force(), is(true)); | ||
assertThat(restoredAssignments.timestamp(), is(1000L)); | ||
} | ||
|
||
private static void assertNodesFromV1(Assignments restoredAssignments) { | ||
assertThat(restoredAssignments.nodes(), hasSize(2)); | ||
List<Assignment> orderedNodes = restoredAssignments.nodes().stream() | ||
.sorted(comparing(Assignment::consistentId)) | ||
.collect(toList()); | ||
|
||
Assignment assignment1 = orderedNodes.get(0); | ||
assertThat(assignment1.consistentId(), is("a")); | ||
assertThat(assignment1.isPeer(), is(true)); | ||
|
||
Assignment assignment2 = orderedNodes.get(1); | ||
assertThat(assignment2.consistentId(), is("b")); | ||
assertThat(assignment2.isPeer(), is(false)); | ||
} | ||
} |
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
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