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 java.io.IOException; 021import java.time.Instant; 022import org.apache.hadoop.hbase.ClockOutOfSyncException; 023import org.apache.hadoop.hbase.master.region.MasterRegion; 024import org.apache.hadoop.hbase.util.Bytes; 025import org.apache.yetus.audience.InterfaceAudience; 026import org.apache.zookeeper.KeeperException; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * Stores the active time of the last active master, so that a new active master can take an active 032 * time greater than that of every earlier one even if its clock is behind theirs. 033 * <p/> 034 * Remote procedures send the active time of the master that started them, and a master rejects a 035 * report carrying an active time greater than its own, as it comes from a procedure started by a 036 * newer master. See {@code MasterRpcServices#throwOnOldMaster}. 037 * <p/> 038 * As a result the active time can be ahead of the wall clock of the current master: if an earlier 039 * active master's clock was ahead, later active times follow the stored value until the clock 040 * catches up with it. 041 */ 042@InterfaceAudience.Private 043public class MasterActiveTimeStore extends MasterStateStore { 044 045 private static final Logger LOG = LoggerFactory.getLogger(MasterActiveTimeStore.class); 046 047 public static final String STATE_NAME = "master_active_time"; 048 049 public MasterActiveTimeStore(MasterRegion masterRegion) throws IOException, KeeperException { 050 super(masterRegion, STATE_NAME, null, null); 051 } 052 053 /** 054 * Returns an active time for a master becoming active: the given time if it is greater than the 055 * last stored active time, or else the last stored active time plus one. The returned time is 056 * stored before returning. 057 * <p/> 058 * If the last stored active time is ahead of the given time by more than {@code maxClockSkewMs}, 059 * the clocks of the masters are too far apart to continue, and this throws 060 * {@link ClockOutOfSyncException} without storing anything. 061 * <p/> 062 * Must only be called by the active master, after the master local region has been opened, so 063 * that an earlier active master can no longer update the stored time. 064 * @param now the current time of this master 065 * @param maxClockSkewMs how far the last stored active time may be ahead of {@code now} 066 * @return the active time of this master 067 * @throws ClockOutOfSyncException if the last stored active time is too far ahead of {@code now} 068 */ 069 public long advance(long now, long maxClockSkewMs) throws IOException { 070 byte[] state = getState(); 071 long activeTime = now; 072 if (state != null && state.length != Bytes.SIZEOF_LONG) { 073 LOG.warn("Ignoring stored master active time of unexpected length {}", state.length); 074 } else if (state != null) { 075 long last = Bytes.toLong(state); 076 if (last - now > maxClockSkewMs) { 077 throw new ClockOutOfSyncException("The last active master became active at " + last + " (" 078 + Instant.ofEpochMilli(last) + "), " + (last - now) + "ms after the current time " + now 079 + " (" + Instant.ofEpochMilli(now) + ") of this master, which is more than the max" 080 + " allowed clock skew of " + maxClockSkewMs + "ms (" + ServerManager.MAX_CLOCK_SKEW_MS 081 + "); check the clocks of the masters"); 082 } 083 if (now <= last) { 084 LOG.warn( 085 "The last active master became active at {} ({}), which is not before the current time" 086 + " {} ({}) of this master, possibly because of clock skew between the masters;" 087 + " using {} as active time", 088 last, Instant.ofEpochMilli(last), now, Instant.ofEpochMilli(now), last + 1); 089 activeTime = last + 1; 090 } 091 } 092 setState(Bytes.toBytes(activeTime)); 093 return activeTime; 094 } 095}