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