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.chukwa.datacollection.collector;
19  
20  import junit.framework.TestCase;
21  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
22  import org.apache.hadoop.chukwa.datacollection.collector.servlet.ServletCollector;
23  import org.apache.hadoop.chukwa.datacollection.connector.http.HttpConnector;
24  import org.apache.hadoop.chukwa.datacollection.sender.RetryListOfCollectors;
25  import org.apache.hadoop.chukwa.datacollection.writer.NullWriter;
26  import org.apache.hadoop.chukwa.datacollection.writer.PipelineStageWriter;
27  import org.apache.hadoop.chukwa.util.ConstRateAdaptor;
28  import org.apache.hadoop.conf.Configuration;
29  import org.mortbay.jetty.Server;
30  import org.mortbay.jetty.servlet.Context;
31  import org.mortbay.jetty.servlet.ServletHolder;
32  
33  public class TestBackpressure extends TestCase {
34  
35    int PORTNO = 9991;
36    
37    /**
38     * NOTE THAT WRITE-RATE * POST SIZE MUST BE GREATER THAN TEST DURATION
39     * 
40     * Default max post size is 2 MB; need to process that several times during test.
41     */
42    int TEST_DURATION_SECS = 40;
43    int WRITE_RATE_KB = 200; //kb/sec
44    
45    
46    int SEND_RATE = 2500* 1000; //bytes/sec
47    int MIN_ACCEPTABLE_PERCENT = 60;
48    
49    public void testBackpressure() throws Exception {
50      Configuration conf = new Configuration();
51      conf.set("chukwaCollector.writerClass", NullWriter.class
52          .getCanonicalName());
53      conf.set(NullWriter.RATE_OPT_NAME, ""+WRITE_RATE_KB);//kb/sec
54      conf.setInt(HttpConnector.MIN_POST_INTERVAL_OPT, 100);
55      conf.setInt("constAdaptor.sleepVariance", 1);
56      conf.setInt("constAdaptor.minSleep", 50);
57      
58      conf.setInt("chukwaAgent.control.port", 0);
59      ChukwaAgent agent = new ChukwaAgent(conf);
60      RetryListOfCollectors clist = new RetryListOfCollectors(conf);
61      clist.add("http://localhost:"+PORTNO+"/chukwa");
62      HttpConnector conn = new HttpConnector(agent);
63      conn.setCollectors(clist);
64      conn.start();
65      Server server = new Server(PORTNO);
66      Context root = new Context(server, "/", Context.SESSIONS);
67  
68      root.addServlet(new ServletHolder(new ServletCollector(conf)), "/*");
69      server.start();
70      server.setStopAtShutdown(false);
71      Thread.sleep(1000);
72      agent.processAddCommand("add adaptor_constSend = " + ConstRateAdaptor.class.getCanonicalName() + 
73          " testData "+ SEND_RATE + " 0");
74      assertNotNull(agent.getAdaptor("adaptor_constSend"));
75      Thread.sleep(TEST_DURATION_SECS * 1000);
76  
77      String[] stat = agent.getAdaptorList().get("adaptor_constSend").split(" ");
78      long kbytesPerSec = Long.valueOf(stat[stat.length -1]) / TEST_DURATION_SECS / 1000;
79      System.out.println("data rate was " + kbytesPerSec + " kb /second");
80      assertTrue(kbytesPerSec < WRITE_RATE_KB); //write rate should throttle sends
81      assertTrue(kbytesPerSec > MIN_ACCEPTABLE_PERCENT* WRITE_RATE_KB / 100);//an assumption, but should hold true
82      agent.shutdown();
83    }
84  }