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.chaos.actions; 019 020import java.util.Collections; 021import java.util.Map; 022import java.util.concurrent.ThreadLocalRandom; 023import org.apache.hadoop.hbase.HBaseTestingUtil; 024import org.apache.hadoop.hbase.TableName; 025import org.apache.hadoop.hbase.client.Admin; 026import org.apache.hadoop.hbase.client.SnapshotType; 027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap; 032 033/** 034 * Action that tries to take a snapshot of a table. 035 */ 036public class SnapshotTableAction extends Action { 037 private static final Logger LOG = LoggerFactory.getLogger(SnapshotTableAction.class); 038 private final TableName tableName; 039 private final long sleepTime; 040 private final Map<String, Object> snapshotProps; 041 042 public SnapshotTableAction(TableName tableName, long ttl) { 043 this(-1, tableName, ttl); 044 } 045 046 public SnapshotTableAction(int sleepTime, TableName tableName, long ttl) { 047 this.tableName = tableName; 048 this.sleepTime = sleepTime; 049 if (ttl > 0) { 050 snapshotProps = ImmutableMap.of("TTL", ttl); 051 } else { 052 snapshotProps = Collections.emptyMap(); 053 } 054 } 055 056 @Override 057 protected Logger getLogger() { 058 return LOG; 059 } 060 061 @Override 062 public void perform() throws Exception { 063 HBaseTestingUtil util = context.getHBaseIntegrationTestingUtility(); 064 String snapshotName = tableName + "-it-" + EnvironmentEdgeManager.currentTime(); 065 Admin admin = util.getAdmin(); 066 067 // Don't try the snapshot if we're stopping 068 if (context.isStopping()) { 069 return; 070 } 071 072 getLogger().info("Performing action: Snapshot table {}", tableName); 073 SnapshotType type = 074 ThreadLocalRandom.current().nextBoolean() ? SnapshotType.FLUSH : SnapshotType.SKIPFLUSH; 075 admin.snapshot(snapshotName, tableName, type, snapshotProps); 076 if (sleepTime > 0) { 077 Thread.sleep(sleepTime); 078 } 079 } 080}