|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.procedure.impl.partition; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TDataNodeConfiguration; |
| 23 | +import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation; |
| 24 | +import org.apache.iotdb.common.rpc.thrift.TEndPoint; |
| 25 | +import org.apache.iotdb.common.rpc.thrift.TNodeResource; |
| 26 | +import org.apache.iotdb.commons.partition.DataPartitionTable; |
| 27 | +import org.apache.iotdb.commons.partition.DatabaseScopedDataPartitionTable; |
| 28 | +import org.apache.iotdb.confignode.procedure.Procedure; |
| 29 | +import org.apache.iotdb.confignode.procedure.store.ProcedureFactory; |
| 30 | + |
| 31 | +import org.apache.tsfile.utils.PublicBAOS; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.io.DataOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.ByteBuffer; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.HashSet; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Set; |
| 44 | + |
| 45 | +public class DataPartitionTableIntegrityCheckProcedureTest { |
| 46 | + @Test |
| 47 | + public void serDeTest() throws IOException { |
| 48 | + DataPartitionTableIntegrityCheckProcedure original = createTestProcedureWithData(); |
| 49 | + |
| 50 | + try (PublicBAOS baos = new PublicBAOS(); |
| 51 | + DataOutputStream dos = new DataOutputStream(baos)) { |
| 52 | + |
| 53 | + original.serialize(dos); |
| 54 | + |
| 55 | + System.out.println("Serialized bytes length: " + baos.size()); |
| 56 | + |
| 57 | + ByteBuffer buffer = ByteBuffer.wrap(baos.getBuf(), 0, baos.size()); |
| 58 | + |
| 59 | + Procedure<?> recreated = ProcedureFactory.getInstance().create(buffer); |
| 60 | + |
| 61 | + if (recreated instanceof DataPartitionTableIntegrityCheckProcedure) { |
| 62 | + DataPartitionTableIntegrityCheckProcedure actual = |
| 63 | + (DataPartitionTableIntegrityCheckProcedure) recreated; |
| 64 | + assertProcedureEquals(original, actual); |
| 65 | + System.out.println("All checked fields match!"); |
| 66 | + } else { |
| 67 | + Assert.fail("Recreated is not DataPartitionTableIntegrityCheckProcedure"); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private DataPartitionTableIntegrityCheckProcedure createTestProcedureWithData() { |
| 73 | + DataPartitionTableIntegrityCheckProcedure proc = |
| 74 | + new DataPartitionTableIntegrityCheckProcedure(); |
| 75 | + String database = "root.test"; |
| 76 | + |
| 77 | + Map<String, Long> earliestTimeslots = new HashMap<>(); |
| 78 | + earliestTimeslots.put(database, 0L); |
| 79 | + proc.setEarliestTimeslots(earliestTimeslots); |
| 80 | + |
| 81 | + Map<Integer, List<DatabaseScopedDataPartitionTable>> dataPartitionTables = new HashMap<>(); |
| 82 | + DataPartitionTable dataPartitionTable = new DataPartitionTable(); |
| 83 | + dataPartitionTables.put( |
| 84 | + 1, |
| 85 | + Collections.singletonList( |
| 86 | + new DatabaseScopedDataPartitionTable(database, dataPartitionTable))); |
| 87 | + proc.setDataPartitionTables(dataPartitionTables); |
| 88 | + |
| 89 | + Set<String> databasesWithLostDataPartition = new HashSet<>(); |
| 90 | + databasesWithLostDataPartition.add(database); |
| 91 | + proc.setDatabasesWithLostDataPartition(databasesWithLostDataPartition); |
| 92 | + |
| 93 | + Map<String, DataPartitionTable> finalDataPartitionTables = new HashMap<>(); |
| 94 | + finalDataPartitionTables.put(database, dataPartitionTable); |
| 95 | + proc.setFinalDataPartitionTables(finalDataPartitionTables); |
| 96 | + |
| 97 | + Set<TDataNodeConfiguration> skipNodes = getTDataNodeConfigurations(1); |
| 98 | + proc.setSkipDataNodes(skipNodes); |
| 99 | + |
| 100 | + Set<TDataNodeConfiguration> failedNodes = getTDataNodeConfigurations(2); |
| 101 | + proc.setFailedDataNodes(failedNodes); |
| 102 | + |
| 103 | + return proc; |
| 104 | + } |
| 105 | + |
| 106 | + private static Set<TDataNodeConfiguration> getTDataNodeConfigurations(int dataNodeId) { |
| 107 | + Set<TDataNodeConfiguration> nodes = new HashSet<>(); |
| 108 | + TDataNodeLocation tDataNodeConfiguration = |
| 109 | + new TDataNodeLocation( |
| 110 | + dataNodeId, |
| 111 | + new TEndPoint("127.0.0.1", 5), |
| 112 | + new TEndPoint("127.0.0.1", 6), |
| 113 | + new TEndPoint("127.0.0.1", 7), |
| 114 | + new TEndPoint("127.0.0.1", 8), |
| 115 | + new TEndPoint("127.0.0.1", 9)); |
| 116 | + TNodeResource resource = new TNodeResource(16, 34359738368L); |
| 117 | + TDataNodeConfiguration skipDataNodeConfiguration = |
| 118 | + new TDataNodeConfiguration(tDataNodeConfiguration, resource); |
| 119 | + nodes.add(skipDataNodeConfiguration); |
| 120 | + return nodes; |
| 121 | + } |
| 122 | + |
| 123 | + private void assertProcedureEquals( |
| 124 | + DataPartitionTableIntegrityCheckProcedure expected, |
| 125 | + DataPartitionTableIntegrityCheckProcedure actual) { |
| 126 | + Assert.assertEquals("procId mismatch", expected.getProcId(), actual.getProcId()); |
| 127 | + Assert.assertEquals("state mismatch", expected.getState(), actual.getState()); |
| 128 | + Assert.assertEquals( |
| 129 | + "earliestTimeslots mismatch", |
| 130 | + expected.getEarliestTimeslots(), |
| 131 | + actual.getEarliestTimeslots()); |
| 132 | + Assert.assertEquals( |
| 133 | + "dataPartitionTables mismatch", |
| 134 | + expected.getDataPartitionTables(), |
| 135 | + actual.getDataPartitionTables()); |
| 136 | + Assert.assertEquals( |
| 137 | + "databasesWithLostDataPartition mismatch", |
| 138 | + expected.getDatabasesWithLostDataPartition(), |
| 139 | + actual.getDatabasesWithLostDataPartition()); |
| 140 | + Assert.assertEquals( |
| 141 | + "finalDataPartitionTables mismatch", |
| 142 | + expected.getFinalDataPartitionTables(), |
| 143 | + actual.getFinalDataPartitionTables()); |
| 144 | + Assert.assertEquals( |
| 145 | + "skipDataNodes mismatch", expected.getSkipDataNodes(), actual.getSkipDataNodes()); |
| 146 | + Assert.assertEquals( |
| 147 | + "failedDataNodes mismatch", expected.getFailedDataNodes(), actual.getFailedDataNodes()); |
| 148 | + } |
| 149 | +} |
0 commit comments