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  
19  package org.apache.hadoop.hbase.metrics.file;
20  
21  import java.io.File;
22  import java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.Collection;
25  import java.util.Date;
26  import java.util.Map;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.hadoop.fs.FileUtil;
30  import org.apache.hadoop.hbase.SmallTests;
31  
32  import org.junit.*;
33  import org.junit.experimental.categories.Category;
34  
35  import static org.junit.Assert.*;
36  
37  /**
38   * Test for TimeStampingMetricsContext functionality.
39   * (FQN class names are used to suppress javac warnings in imports.)
40   */
41  @Category(SmallTests.class)
42  @SuppressWarnings("deprecation")
43  public class TestTimeStampingMetricsContext {
44  
45    private static final int updatePeriodSeconds = 2;
46  
47    private TimeStampingFileContext mc;
48  
49    @Test
50    public void testFileUpdate() throws Exception {
51      final Date start = new Date();
52      final File metricOutFile = FileUtil.createLocalTempFile(
53        new File(FileUtils.getTempDirectory(),getClass().getName() + "-out-"), "", true);
54      assertTrue(metricOutFile.exists());
55      assertEquals(0L, metricOutFile.length());
56  
57      mc = new TimeStampingFileContext();
58      org.apache.hadoop.metrics.ContextFactory cf
59        = org.apache.hadoop.metrics.ContextFactory.getFactory();
60      cf.setAttribute("test1.fileName", metricOutFile.getAbsolutePath());
61      cf.setAttribute("test1.period", Integer.toString(updatePeriodSeconds));
62      mc.init("test1", cf);
63  
64      assertEquals("test1", mc.getContextName());
65  
66      org.apache.hadoop.metrics.MetricsRecord r = mc.createRecord("testRecord");
67      r.setTag("testTag1", "testTagValue1");
68      r.setTag("testTag2", "testTagValue2");
69      r.setMetric("testMetric1", 1);
70      r.setMetric("testMetric2", 33);
71      r.update();
72  
73      mc.startMonitoring();
74      assertTrue(mc.isMonitoring());
75  
76      // wait 3/2 of the update period:
77      Thread.sleep((1000 * updatePeriodSeconds * 3)/2);
78  
79      mc.stopMonitoring();
80      assertFalse(mc.isMonitoring());
81  
82      mc.close();
83  
84      Map<String, Collection<org.apache.hadoop.metrics.spi.OutputRecord>> m = mc.getAllRecords();
85      assertEquals(1, m.size());
86      Collection<org.apache.hadoop.metrics.spi.OutputRecord> outputRecords = m.get("testRecord");
87      assertNotNull(outputRecords);
88      assertEquals(1, outputRecords.size());
89      org.apache.hadoop.metrics.spi.OutputRecord outputRecord = outputRecords.iterator().next();
90      assertNotNull(outputRecord);
91  
92      String outStr = FileUtils.readFileToString(metricOutFile);
93      assertTrue(outStr.length() > 0);
94      int pos = outStr.indexOf(" ");
95      String time = outStr.substring(0, pos);
96  
97      DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
98      Date date = df.parse(time);
99      assertTrue(date.after(start));
100     assertTrue(date.before(new Date()));
101 
102     String reminder = outStr.substring(pos);
103     assertEquals(" test1.testRecord: testTag1=testTagValue1, testTag2=testTagValue2, testMetric1=1,"
104       +" testMetric2=33\n", reminder);
105   }
106 
107 }