001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.apache.hadoop.hbase.master.region.MasterRegionFactory.STATE_FAMILY;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertThrows;
023
024import org.apache.hadoop.hbase.ClockOutOfSyncException;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.jupiter.api.AfterEach;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033@Tag(MasterTests.TAG)
034@Tag(MediumTests.TAG)
035public class TestMasterActiveTimeStore extends MasterStateStoreTestBase {
036
037  private static final long MAX_SKEW = 30_000;
038
039  @AfterEach
040  public void tearDown() throws Exception {
041    cleanup();
042  }
043
044  @Test
045  public void testFirstActiveMasterUsesItsClock() throws Exception {
046    assertEquals(1000, new MasterActiveTimeStore(REGION).advance(1000, MAX_SKEW));
047  }
048
049  @Test
050  public void testActiveTimeIncreasesWhenClockIsBehind() throws Exception {
051    assertEquals(1000, new MasterActiveTimeStore(REGION).advance(1000, MAX_SKEW));
052    // the next active master's clock is behind the previous one's
053    assertEquals(1001, new MasterActiveTimeStore(REGION).advance(900, MAX_SKEW));
054    // the same time as the last active time
055    assertEquals(1002, new MasterActiveTimeStore(REGION).advance(1001, MAX_SKEW));
056  }
057
058  @Test
059  public void testActiveTimeFollowsClockWhenAhead() throws Exception {
060    assertEquals(1000, new MasterActiveTimeStore(REGION).advance(1000, MAX_SKEW));
061    assertEquals(5000, new MasterActiveTimeStore(REGION).advance(5000, MAX_SKEW));
062    assertEquals(5001, new MasterActiveTimeStore(REGION).advance(4000, MAX_SKEW));
063  }
064
065  @Test
066  public void testFailsWhenClockIsBehindByMoreThanMaxSkew() throws Exception {
067    assertEquals(100_000, new MasterActiveTimeStore(REGION).advance(100_000, MAX_SKEW));
068    // exactly the max skew behind is still allowed
069    assertEquals(100_001, new MasterActiveTimeStore(REGION).advance(70_000, MAX_SKEW));
070    // more than the max skew behind the last active time fails, and stores nothing
071    assertThrows(ClockOutOfSyncException.class,
072      () -> new MasterActiveTimeStore(REGION).advance(70_000, MAX_SKEW));
073    assertEquals(100_002, new MasterActiveTimeStore(REGION).advance(100_000, MAX_SKEW));
074  }
075
076  @Test
077  public void testMalformedValueIsIgnored() throws Exception {
078    REGION.update(r -> r.put(new Put(Bytes.toBytes(MasterActiveTimeStore.STATE_NAME))
079      .addColumn(STATE_FAMILY, Bytes.toBytes("d"), new byte[3])));
080    assertEquals(1000, new MasterActiveTimeStore(REGION).advance(1000, MAX_SKEW));
081    assertEquals(1001, new MasterActiveTimeStore(REGION).advance(900, MAX_SKEW));
082  }
083}