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 org.apache.hadoop.fs.FileStatus;
25  import org.junit.After;
26  import org.junit.AfterClass;
27  import org.junit.Before;
28  import org.junit.BeforeClass;
29  import org.junit.Ignore;
30  import org.junit.Test;
31  
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.fs.Path;
35  import org.apache.hadoop.fs.FileSystem;
36  import org.apache.hadoop.conf.Configuration;
37  
38  import java.net.URLEncoder;
39  import java.util.concurrent.atomic.AtomicBoolean;
40  
41  public class TestOldLogsCleaner {
42  
43    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44  
45  
46    /**
47     * @throws java.lang.Exception
48     */
49    @BeforeClass
50    public static void setUpBeforeClass() throws Exception {
51    }
52  
53    /**
54     * @throws java.lang.Exception
55     */
56    @AfterClass
57    public static void tearDownAfterClass() throws Exception {
58    }
59  
60    /**
61     * @throws java.lang.Exception
62     */
63    @Before
64    public void setUp() throws Exception {
65    }
66  
67    /**
68     * @throws java.lang.Exception
69     */
70    @After
71    public void tearDown() throws Exception {
72    }
73  
74    @Test
75    public void testLogCleaning() throws Exception{
76      Configuration c = TEST_UTIL.getConfiguration();
77      Path oldLogDir = new Path(TEST_UTIL.getTestDir(),
78          HConstants.HREGION_OLDLOGDIR_NAME);
79      String fakeMachineName = URLEncoder.encode("regionserver:60020", "UTF8");
80  
81      FileSystem fs = FileSystem.get(c);
82      AtomicBoolean stop = new AtomicBoolean(false);
83      OldLogsCleaner cleaner = new OldLogsCleaner(1000, stop,c, fs, oldLogDir);
84  
85      // Create 2 invalid files, 1 "recent" file, 1 very new file and 30 old files
86      long now = System.currentTimeMillis();
87      fs.delete(oldLogDir, true);
88      fs.mkdirs(oldLogDir);
89      fs.createNewFile(new Path(oldLogDir, "a"));
90      fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + "a"));
91      fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + now));
92      System.out.println("Now is: " + now);
93      for (int i = 0; i < 30; i++) {
94        fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now - 6000000 - i) ));
95      }
96      for (FileStatus stat : fs.listStatus(oldLogDir)) {
97        System.out.println(stat.getPath().toString());
98      }
99  
100     fs.createNewFile(new Path(oldLogDir, fakeMachineName + "." + (now + 10000) ));
101 
102     assertEquals(34, fs.listStatus(oldLogDir).length);
103 
104     // This will take care of 20 old log files (default max we can delete)
105     cleaner.chore();
106 
107     assertEquals(14, fs.listStatus(oldLogDir).length);
108 
109     // We will delete all remaining log files and those that are invalid
110     cleaner.chore();
111 
112     // We end up with the current log file and a newer one
113     assertEquals(2, fs.listStatus(oldLogDir).length);
114   }
115 
116 }