View Javadoc

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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.cleaner;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.IOException;
23  import java.net.URLEncoder;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.*;
30  import org.apache.hadoop.hbase.catalog.CatalogTracker;
31  import org.apache.hadoop.hbase.replication.ReplicationFactory;
32  import org.apache.hadoop.hbase.replication.ReplicationQueues;
33  import org.apache.hadoop.hbase.replication.regionserver.Replication;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category(MediumTests.class)
42  public class TestLogsCleaner {
43  
44    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45  
46    /**
47     * @throws java.lang.Exception
48     */
49    @BeforeClass
50    public static void setUpBeforeClass() throws Exception {
51      TEST_UTIL.startMiniZKCluster();
52    }
53  
54    /**
55     * @throws java.lang.Exception
56     */
57    @AfterClass
58    public static void tearDownAfterClass() throws Exception {
59      TEST_UTIL.shutdownMiniZKCluster();
60    }
61  
62    @Test
63    public void testLogCleaning() throws Exception{
64      Configuration conf = TEST_UTIL.getConfiguration();
65      // set TTL
66      long ttl = 10000;
67      conf.setLong("hbase.master.logcleaner.ttl", ttl);
68      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
69      Replication.decorateMasterConfiguration(conf);
70      Server server = new DummyServer();
71      ReplicationQueues repQueues =
72          ReplicationFactory.getReplicationQueues(server.getZooKeeper(), conf, server);
73      repQueues.init(server.getServerName().toString());
74      final Path oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
75          HConstants.HREGION_OLDLOGDIR_NAME);
76      String fakeMachineName =
77        URLEncoder.encode(server.getServerName().toString(), "UTF8");
78  
79      final FileSystem fs = FileSystem.get(conf);
80  
81      // Create 2 invalid files, 1 "recent" file, 1 very new file and 30 old files
82      long now = System.currentTimeMillis();
83      fs.delete(oldLogDir, true);
84      fs.mkdirs(oldLogDir);
85      // Case 1: 2 invalid files, which would be deleted directly
86      fs.createNewFile(new Path(oldLogDir, "a"));
87      fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
88      // Case 2: 1 "recent" file, not even deletable for the first log cleaner
89      // (TimeToLiveLogCleaner), so we are not going down the chain
90      System.out.println("Now is: " + now);
91      for (int i = 1; i < 31; i++) {
92        // Case 3: old files which would be deletable for the first log cleaner
93        // (TimeToLiveLogCleaner), and also for the second (ReplicationLogCleaner)
94        Path fileName = new Path(oldLogDir, fakeMachineName + "." + (now - i) );
95        fs.createNewFile(fileName);
96        // Case 4: put 3 old log files in ZK indicating that they are scheduled
97        // for replication so these files would pass the first log cleaner
98        // (TimeToLiveLogCleaner) but would be rejected by the second
99        // (ReplicationLogCleaner)
100       if (i % (30/3) == 1) {
101         repQueues.addLog(fakeMachineName, fileName.getName());
102         System.out.println("Replication log file: " + fileName);
103       }
104     }
105 
106     // sleep for sometime to get newer modifcation time
107     Thread.sleep(ttl);
108     fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
109 
110     // Case 2: 1 newer file, not even deletable for the first log cleaner
111     // (TimeToLiveLogCleaner), so we are not going down the chain
112     fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
113 
114     for (FileStatus stat : fs.listStatus(oldLogDir)) {
115       System.out.println(stat.getPath().toString());
116     }
117 
118     assertEquals(34, fs.listStatus(oldLogDir).length);
119 
120     LogCleaner cleaner  = new LogCleaner(1000, server, conf, fs, oldLogDir);
121     cleaner.chore();
122 
123     // We end up with the current log file, a newer one and the 3 old log
124     // files which are scheduled for replication
125     TEST_UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
126       @Override
127       public boolean evaluate() throws Exception {
128         return 5 == fs.listStatus(oldLogDir).length;
129       }
130     });
131 
132     for (FileStatus file : fs.listStatus(oldLogDir)) {
133       System.out.println("Kept log files: " + file.getPath().getName());
134     }
135   }
136 
137   static class DummyServer implements Server {
138 
139     @Override
140     public Configuration getConfiguration() {
141       return TEST_UTIL.getConfiguration();
142     }
143 
144     @Override
145     public ZooKeeperWatcher getZooKeeper() {
146       try {
147         return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
148       } catch (IOException e) {
149         e.printStackTrace();
150       }
151       return null;
152     }
153 
154     @Override
155     public CatalogTracker getCatalogTracker() {
156       return null;
157     }
158 
159     @Override
160     public ServerName getServerName() {
161       return ServerName.valueOf("regionserver,60020,000000");
162     }
163 
164     @Override
165     public void abort(String why, Throwable e) {}
166 
167     @Override
168     public boolean isAborted() {
169       return false;
170     }
171 
172     @Override
173     public void stop(String why) {}
174 
175     @Override
176     public boolean isStopped() {
177       return false;
178     }
179   }
180 
181 }
182