1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.IOException;
25  import java.net.URLEncoder;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileStatus;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.Server;
35  import org.apache.hadoop.hbase.catalog.CatalogTracker;
36  import org.apache.hadoop.hbase.replication.ReplicationZookeeper;
37  import org.apache.hadoop.hbase.replication.regionserver.Replication;
38  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  
43  public class TestLogsCleaner {
44  
45    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46  
47    /**
48     * @throws java.lang.Exception
49     */
50    @BeforeClass
51    public static void setUpBeforeClass() throws Exception {
52      TEST_UTIL.startMiniZKCluster();
53    }
54  
55    /**
56     * @throws java.lang.Exception
57     */
58    @AfterClass
59    public static void tearDownAfterClass() throws Exception {
60      TEST_UTIL.shutdownMiniZKCluster();
61    }
62  
63    @Test
64    public void testLogCleaning() throws Exception{
65      Configuration conf = TEST_UTIL.getConfiguration();
66      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
67      Replication.decorateMasterConfiguration(conf);
68      Server server = new DummyServer();
69      ReplicationZookeeper zkHelper =
70          new ReplicationZookeeper(server, new AtomicBoolean(true));
71  
72      Path oldLogDir = new Path(HBaseTestingUtility.getTestDir(),
73          HConstants.HREGION_OLDLOGDIR_NAME);
74      String fakeMachineName = URLEncoder.encode(server.getServerName(), "UTF8");
75  
76      FileSystem fs = FileSystem.get(conf);
77      LogCleaner cleaner  = new LogCleaner(1000, server, conf, fs, oldLogDir);
78  
79      // Create 2 invalid files, 1 "recent" file, 1 very new file and 30 old files
80      long now = System.currentTimeMillis();
81      fs.delete(oldLogDir, true);
82      fs.mkdirs(oldLogDir);
83      // Case 1: 2 invalid files, which would be deleted directly
84      fs.createNewFile(new Path(oldLogDir, "a"));
85      fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
86      // Case 2: 1 "recent" file, not even deletable for the first log cleaner
87      // (TimeToLiveLogCleaner), so we are not going down the chain
88      fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
89      System.out.println("Now is: " + now);
90      for (int i = 0; i < 30; i++) {
91        // Case 3: old files which would be deletable for the first log cleaner
92        // (TimeToLiveLogCleaner), and also for the second (ReplicationLogCleaner)
93        Path fileName = new Path(oldLogDir, fakeMachineName + "." +
94            (now - 6000000 - 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) == 0) {
101         zkHelper.addLogToList(fileName.getName(), fakeMachineName);
102         System.out.println("Replication log file: " + fileName);
103       }
104     }
105     for (FileStatus stat : fs.listStatus(oldLogDir)) {
106       System.out.println(stat.getPath().toString());
107     }
108 
109     // Case 2: 1 newer file, not even deletable for the first log cleaner
110     // (TimeToLiveLogCleaner), so we are not going down the chain
111     fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
112 
113     assertEquals(34, fs.listStatus(oldLogDir).length);
114 
115     cleaner.chore();
116 
117     // We end up with the current log file, a newer one and the 3 old log
118     // files which are scheduled for replication
119     assertEquals(5, fs.listStatus(oldLogDir).length);
120 
121     for (FileStatus file : fs.listStatus(oldLogDir)) {
122       System.out.println("Kept log files: " + file.getPath().getName());
123     }
124   }
125 
126   static class DummyServer implements Server {
127 
128     @Override
129     public Configuration getConfiguration() {
130       return TEST_UTIL.getConfiguration();
131     }
132 
133     @Override
134     public ZooKeeperWatcher getZooKeeper() {
135       try {
136         return new ZooKeeperWatcher(getConfiguration(), "dummy server", this);
137       } catch (IOException e) {
138         e.printStackTrace();
139       }
140       return null;
141     }
142 
143     @Override
144     public CatalogTracker getCatalogTracker() {
145       return null;
146     }
147 
148     @Override
149     public String getServerName() {
150       return "regionserver,60020,000000";
151     }
152 
153     @Override
154     public void abort(String why, Throwable e) {}
155 
156     @Override
157     public void stop(String why) {}
158 
159     @Override
160     public boolean isStopped() {
161       return false;
162     }
163   }
164 }