/** * 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.hadoop.hdfs.qjournal.client; import static org.junit.Assert.*; import java.util.Map; import java.util.Map.Entry; import org.apache.hadoop.hdfs.qjournal.protocol.QJournalProtocolProtos.PrepareRecoveryResponseProto; import org.apache.hadoop.hdfs.qjournal.protocol.QJournalProtocolProtos.SegmentStateProto; import org.junit.Test; import org.mockito.Mockito; import com.google.common.collect.Maps; import static org.apache.hadoop.hdfs.qjournal.client.SegmentRecoveryComparator.INSTANCE; public class TestSegmentRecoveryComparator { private static Map.Entry<AsyncLogger, PrepareRecoveryResponseProto> makeEntry( PrepareRecoveryResponseProto proto) { return Maps.immutableEntry(Mockito.mock(AsyncLogger.class), proto); } private static PrepareRecoveryResponseProto makeSSPInstance(long startTxId, long endTxId, boolean isInProgress) { PrepareRecoveryResponseProto prrp = new PrepareRecoveryResponseProto(); prrp.setSegmentState(new SegmentStateProto(startTxId, endTxId, isInProgress)); return prrp; } @Test public void testComparisons() { PrepareRecoveryResponseProto p1 = makeSSPInstance(1L, 3L, true); p1.setLastWriterEpoch(0L); Entry<AsyncLogger, PrepareRecoveryResponseProto> INPROGRESS_1_3 = makeEntry(p1); PrepareRecoveryResponseProto p2 = makeSSPInstance(1L, 4L, true); p2.setLastWriterEpoch(0L); Entry<AsyncLogger, PrepareRecoveryResponseProto> INPROGRESS_1_4 = makeEntry(p2); PrepareRecoveryResponseProto p3 = makeSSPInstance(1L, 4L, true); p3.setLastWriterEpoch(0L); p3.setAcceptedInEpoch(1L); Entry<AsyncLogger, PrepareRecoveryResponseProto> INPROGRESS_1_4_ACCEPTED = makeEntry(p3); PrepareRecoveryResponseProto p4 = makeSSPInstance(1L, 3L, false); p4.setLastWriterEpoch(0L); Entry<AsyncLogger, PrepareRecoveryResponseProto> FINALIZED_1_3 = makeEntry(p4); // Should compare equal to itself assertEquals(0, INSTANCE.compare(INPROGRESS_1_3, INPROGRESS_1_3)); // Longer log wins. assertEquals(-1, INSTANCE.compare(INPROGRESS_1_3, INPROGRESS_1_4)); assertEquals(1, INSTANCE.compare(INPROGRESS_1_4, INPROGRESS_1_3)); // Finalized log wins even over a longer in-progress assertEquals(-1, INSTANCE.compare(INPROGRESS_1_4, FINALIZED_1_3)); assertEquals(1, INSTANCE.compare(FINALIZED_1_3, INPROGRESS_1_4)); // Finalized log wins even if the in-progress one has an accepted // recovery proposal. assertEquals(-1, INSTANCE.compare(INPROGRESS_1_4_ACCEPTED, FINALIZED_1_3)); assertEquals(1, INSTANCE.compare(FINALIZED_1_3, INPROGRESS_1_4_ACCEPTED)); } }