View Javadoc

1   package org.apache.helix.monitoring.mbeans;
2   
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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.UUID;
26  
27  import org.apache.helix.HelixDataAccessor;
28  import org.apache.helix.Mocks;
29  import org.apache.helix.NotificationContext;
30  import org.apache.helix.PropertyType;
31  import org.apache.helix.ZNRecord;
32  import org.apache.helix.model.LiveInstance.LiveInstanceProperty;
33  import org.apache.helix.tools.DefaultIdealStateCalculator;
34  import org.testng.annotations.Test;
35  
36  
37  
38  public class TestClusterStatusMonitor
39  {
40    List<String> _instances;
41    List<ZNRecord> _liveInstances;
42    String _db = "DB";
43    String _db2 = "TestDB";
44    int _replicas = 3;
45    int _partitions = 50;
46    ZNRecord _externalView, _externalView2;
47  
48    class MockDataAccessor extends Mocks.MockAccessor
49    {
50      public MockDataAccessor()
51      {
52        _instances = new ArrayList<String>();
53        for(int i = 0;i < 5; i++)
54        {
55          String instance = "localhost_"+(12918+i);
56          _instances.add(instance);
57        }
58        ZNRecord externalView = DefaultIdealStateCalculator.calculateIdealState(
59            _instances, _partitions, _replicas, _db, "MASTER", "SLAVE");
60  
61        ZNRecord externalView2 = DefaultIdealStateCalculator.calculateIdealState(
62            _instances, 80, 2, _db2, "MASTER", "SLAVE");
63  
64      }
65      public ZNRecord getProperty(PropertyType type, String resource)
66      {
67        if(type == PropertyType.IDEALSTATES || type == PropertyType.EXTERNALVIEW)
68        {
69          if(resource.equals(_db))
70          {
71            return _externalView;
72          }
73          else if(resource.equals(_db2))
74          {
75            return _externalView2;
76          }
77        }
78        return null;
79      }
80    }
81    class MockHelixManager extends Mocks.MockManager
82    {
83      MockDataAccessor _accessor = new MockDataAccessor();
84  
85      @Override
86  		public HelixDataAccessor getHelixDataAccessor()
87      {
88        return _accessor;
89      }
90  
91    }
92    @Test()
93    public void TestReportData()
94    {
95    	System.out.println("START TestClusterStatusMonitor at" + new Date(System.currentTimeMillis()));
96      List<String> _instances;
97      List<ZNRecord> _liveInstances = new ArrayList<ZNRecord>();
98      String _db = "DB";
99      int _replicas = 3;
100     int _partitions = 50;
101 
102     _instances = new ArrayList<String>();
103     for(int i = 0;i < 5; i++)
104     {
105       String instance = "localhost_"+(12918+i);
106       _instances.add(instance);
107       ZNRecord metaData = new ZNRecord(instance);
108       metaData.setSimpleField(LiveInstanceProperty.SESSION_ID.toString(),
109           UUID.randomUUID().toString());
110       _liveInstances.add(metaData);
111     }
112     ZNRecord externalView = DefaultIdealStateCalculator.calculateIdealState(
113         _instances, _partitions, _replicas, _db, "MASTER", "SLAVE");
114 
115     ZNRecord externalView2 = DefaultIdealStateCalculator.calculateIdealState(
116         _instances, 80, 2, "TestDB", "MASTER", "SLAVE");
117 
118     List<ZNRecord> externalViews = new ArrayList<ZNRecord>();
119     externalViews.add(externalView);
120     externalViews.add(externalView2);
121 
122     ClusterStatusMonitor monitor = new ClusterStatusMonitor("cluster1");
123     MockHelixManager manager = new MockHelixManager();
124     NotificationContext context = new NotificationContext(manager);
125     System.out.println("END TestClusterStatusMonitor at" + new Date(System.currentTimeMillis()));
126   }
127 }