private static void checkEnum() throws Exception { String type = (String) server.getAttribute(heapPool, "Type"); if (!type.equals("HEAP")) { throw new RuntimeException("TEST FAILED: " + " incorrect memory type for " + heapPool); } type = (String) server.getAttribute(nonHeapPool, "Type"); if (!type.equals("NON_HEAP")) { throw new RuntimeException("TEST FAILED: " + " incorrect memory type for " + nonHeapPool); } }
public void testMbeans() throws MalformedObjectNameException, BundleException, InstanceNotFoundException, ReflectionException, MBeanException, AttributeNotFoundException { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName digraphName = new ObjectName(REGION_DOMAIN_PROP + ":type=RegionDigraph,*"); ObjectName regionNameAllQuery = new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=*,*"); Set<ObjectInstance> digraphs = server.queryMBeans(null, digraphName); assertEquals("Expected only one instance of digraph", 1, digraphs.size()); Set<ObjectInstance> regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Expected only one instance of region", 1, regions.size()); Region pp1Region = digraph.createRegion(PP1); Bundle pp1Bundle = bundleInstaller.installBundle(PP1, pp1Region); Region sp1Region = digraph.createRegion(SP1); Bundle sp1Bundle = bundleInstaller.installBundle(SP1, sp1Region); regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Wrong number of regions", 3, regions.size()); Set<ObjectInstance> pp1Query = server.queryMBeans(null, new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=" + PP1 + ",*")); assertEquals("Expected only one instance of: " + PP1, 1, pp1Query.size()); Set<ObjectInstance> sp1Query = server.queryMBeans(null, new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=" + SP1 + ",*")); assertEquals("Expected only one instance of: " + SP1, 1, sp1Query.size()); ObjectName pp1Name = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {PP1}, new String[] {String.class.getName()}); assertEquals(PP1 + " regions not equal.", pp1Query.iterator().next().getObjectName(), pp1Name); ObjectName sp1Name = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {SP1}, new String[] {String.class.getName()}); assertEquals(SP1 + " regions not equal.", sp1Query.iterator().next().getObjectName(), sp1Name); // test non existing region ObjectName shouldNotExistName = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {"ShouldNotExist"}, new String[] {String.class.getName()}); assertNull("Should not exist", shouldNotExistName); long[] bundleIds = (long[]) server.getAttribute(pp1Name, "BundleIds"); assertEquals("Wrong number of bundles", 1, bundleIds.length); assertEquals("Wrong bundle", pp1Bundle.getBundleId(), bundleIds[0]); String name = (String) server.getAttribute(pp1Name, "Name"); assertEquals("Wrong name", PP1, name); bundleIds = (long[]) server.getAttribute(sp1Name, "BundleIds"); assertEquals("Wrong number of bundles", 1, bundleIds.length); assertEquals("Wrong bundle", sp1Bundle.getBundleId(), bundleIds[0]); name = (String) server.getAttribute(sp1Name, "Name"); assertEquals("Wrong name", SP1, name); regionBundle.stop(); // Now make sure we have no mbeans digraphs = server.queryMBeans(digraphName, null); assertEquals("Wrong number of digraphs", 0, digraphs.size()); regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Wrong number of regions", 0, regions.size()); }
private static void checkThreadInfo() throws Exception { // assume all threads stay alive long[] ids = (long[]) server.getAttribute(thread, "AllThreadIds"); Object result = server.invoke(thread, "getThreadInfo", new Object[] {ids}, new String[] {"[J"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } result = server.invoke( thread, "getThreadInfo", new Object[] {ids, new Integer(2)}, new String[] {"[J", "int"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } long id = Thread.currentThread().getId(); result = server.invoke(thread, "getThreadInfo", new Object[] {new Long(id)}, new String[] {"long"}); printThreadInfo((CompositeData) result); result = server.invoke( thread, "getThreadInfo", new Object[] {new Long(id), new Integer(2)}, new String[] {"long", "int"}); printThreadInfo((CompositeData) result); }
private static void checkMemoryUsage() throws Exception { // sanity check to have non-zero usage Object u1 = server.getAttribute(memory, "HeapMemoryUsage"); Object u2 = server.getAttribute(memory, "NonHeapMemoryUsage"); Object u3 = server.getAttribute(heapPool, "Usage"); Object u4 = server.getAttribute(nonHeapPool, "Usage"); if (getCommitted(u1) <= 0 || getCommitted(u2) <= 0 || getCommitted(u3) <= 0 || getCommitted(u4) <= 0) { throw new RuntimeException("TEST FAILED: " + " expected non-zero committed usage"); } server.invoke(memory, "gc", new Object[0], new String[0]); Object u5 = server.getAttribute(heapPool, "CollectionUsage"); if (getCommitted(u5) <= 0) { throw new RuntimeException("TEST FAILED: " + " expected non-zero committed collected usage"); } }
private boolean isModified(ObjectName name) { boolean modified = false; if (name != null) { try { modified = (Boolean) server.getAttribute(name, "Modified"); } catch (Exception e) { // Okay to fail } } return modified; }
private int getState(ObjectName name) { int status = -1; if (name != null) { try { status = (Integer) server.getAttribute(name, "State"); } catch (Exception e) { log.warn("getState", e); } } return status; }
private static void checkSunGC() throws Exception { // Test com.sun.management proxy List<GarbageCollectorMXBean> gcs = getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean gc : gcs) { ObjectName sunGc = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",name=" + gc.getName()); CompositeData cd = (CompositeData) server.getAttribute(sunGc, "LastGcInfo"); if (cd != null) { System.out.println("GC statistic for : " + gc.getName()); printGcInfo(cd); } } }
@Test(expectedExceptions = IllegalStateException.class) void registerMBeanFailed2() throws NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanException, MalformedObjectNameException, AttributeNotFoundException, ReflectionException, InstanceNotFoundException { MBeanServer server = EasyMock.createMock(MBeanServer.class); ObjectName oName = new ObjectName(JolokiaMBeanServerHolderMBean.OBJECT_NAME); EasyMock.expect(server.registerMBean(EasyMock.anyObject(), EasyMock.eq(oName))) .andThrow(new InstanceAlreadyExistsException()); EasyMock.expect( server.getAttribute( EasyMock.eq(oName), eq(JolokiaMBeanServerUtil.JOLOKIA_MBEAN_SERVER_ATTRIBUTE))) .andThrow(new AttributeNotFoundException()); EasyMock.replay(server); MBeanServer m = JolokiaMBeanServerUtil.registerJolokiaMBeanServerHolderMBean(server); }
private static void checkList() throws Exception { String[] args = (String[]) server.getAttribute(runtime, "InputArguments"); if (args.length < 1) { throw new RuntimeException("TEST FAILED: " + " empty input arguments"); } // check if -verbose:gc exists boolean found = false; for (String option : args) { if (option.equals(OPTION)) { found = true; break; } } if (!found) { throw new RuntimeException("TEST FAILED: " + "VM option " + OPTION + " not found"); } }
@Test void registerMBean2() throws NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanException, MalformedObjectNameException, AttributeNotFoundException, ReflectionException, InstanceNotFoundException { MBeanServer server = EasyMock.createMock(MBeanServer.class); MBeanServer ret = MBeanServerFactory.newMBeanServer(); ObjectName oName = new ObjectName(JolokiaMBeanServerHolderMBean.OBJECT_NAME); EasyMock.expect(server.registerMBean(EasyMock.anyObject(), EasyMock.eq(oName))) .andThrow(new InstanceAlreadyExistsException()); EasyMock.expect( server.getAttribute( EasyMock.eq(oName), eq(JolokiaMBeanServerUtil.JOLOKIA_MBEAN_SERVER_ATTRIBUTE))) .andReturn(ret); EasyMock.replay(server); MBeanServer m = JolokiaMBeanServerUtil.registerJolokiaMBeanServerHolderMBean(server); Assert.assertEquals(ret, m); }
private long persist(File f, ObjectName name) { long deployed = f.lastModified(); try { Element e = (Element) server.getAttribute(name, "Persist"); if (e != null) { XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); Document doc = new Document(); e.detach(); doc.setRootElement(e); File tmp = new File(f.getAbsolutePath() + ".tmp"); FileWriter writer = new FileWriter(tmp); out.output(doc, writer); writer.close(); f.delete(); tmp.renameTo(f); deployed = f.lastModified(); } } catch (Exception ex) { log.warn("persist", ex); } return deployed; }
private static void checkMap() throws Exception { // Add new system properties System.setProperty(KEY1, VALUE1); System.setProperty(KEY2, VALUE2); TabularData props1 = (TabularData) server.getAttribute(runtime, "SystemProperties"); String value1 = getProperty(props1, KEY1); if (value1 == null || !value1.equals(VALUE1)) { throw new RuntimeException( "TEST FAILED: " + KEY1 + " property found" + " with value = " + value1 + " but expected to be " + VALUE1); } String value2 = getProperty(props1, KEY2); if (value2 == null || !value2.equals(VALUE2)) { throw new RuntimeException( "TEST FAILED: " + KEY2 + " property found" + " with value = " + value2 + " but expected to be " + VALUE2); } String value3 = getProperty(props1, KEY3); if (value3 != null) { throw new RuntimeException( "TEST FAILED: " + KEY3 + " property found" + " but should not exist"); } }
// The Run Method. // Started by main() on a single thread, this code publishes Cloud membership // to the Cloud once a second (across all members). If anybody disagrees // with the membership Heartbeat, they will start a round of Paxos group // discovery. public void run() { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName os; try { os = new ObjectName("java.lang:type=OperatingSystem"); } catch (MalformedObjectNameException e) { throw Log.errRTExcept(e); } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); int counter = 0; while (true) { // Once per second, for the entire cloud a Node will multi-cast publish // itself, so other unrelated Clouds discover each other and form up. try { Thread.sleep(SLEEP); } // Only once-sec per entire Cloud catch (InterruptedException e) { } // Update the interesting health self-info for publication also H2O cloud = H2O.CLOUD; HeartBeat hb = H2O.SELF._heartbeat; hb._hb_version = HB_VERSION++; hb._jvm_boot_msec = TimeLine.JVM_BOOT_MSEC; final Runtime run = Runtime.getRuntime(); hb.set_free_mem(run.freeMemory()); hb.set_max_mem(run.maxMemory()); hb.set_tot_mem(run.totalMemory()); hb._keys = (H2O.STORE.size()); hb.set_valsz(myHisto.histo(false)._cached); hb._num_cpus = (char) run.availableProcessors(); if (counter % 300 == 2) { // run mini-benchmark every 5 mins hb._gflops = Linpack.run(); hb._membw = MemoryBandwidth.run(); } Object load = null; try { load = mbs.getAttribute(os, "SystemLoadAverage"); } catch (Exception e) { // Ignore, data probably not available on this VM } hb._system_load_average = load instanceof Double ? ((Double) load).floatValue() : 0; int rpcs = 0; for (H2ONode h2o : cloud._memary) rpcs += h2o.taskSize(); hb._rpcs = (char) rpcs; // Scrape F/J pool counts hb._fjthrds = new short[H2O.MAX_PRIORITY + 1]; hb._fjqueue = new short[H2O.MAX_PRIORITY + 1]; for (int i = 0; i < hb._fjthrds.length; i++) { hb._fjthrds[i] = (short) H2O.getWrkThrPoolSize(i); hb._fjqueue[i] = (short) H2O.getWrkQueueSize(i); } hb._tcps_active = (char) H2ONode.TCPS.get(); // get the usable and total disk storage for the partition where the // persistent KV pairs are stored hb.set_free_disk(Persist.getIce().getUsableSpace()); hb.set_max_disk(Persist.getIce().getTotalSpace()); // get cpu utilization for the system and for this process. (linux only.) LinuxProcFileReader lpfr = new LinuxProcFileReader(); lpfr.read(); if (lpfr.valid()) { hb._system_idle_ticks = lpfr.getSystemIdleTicks(); hb._system_total_ticks = lpfr.getSystemTotalTicks(); hb._process_total_ticks = lpfr.getProcessTotalTicks(); hb._process_num_open_fds = lpfr.getProcessNumOpenFds(); } else { hb._system_idle_ticks = -1; hb._system_total_ticks = -1; hb._process_total_ticks = -1; hb._process_num_open_fds = -1; } hb._pid = lpfr.getProcessID(); // Announce what Cloud we think we are in. // Publish our health as well. UDPHeartbeat.build_and_multicast(cloud, hb); // If we have no internet connection, then the multicast goes // nowhere and we never receive a heartbeat from ourselves! // Fake it now. long now = System.currentTimeMillis(); H2O.SELF._last_heard_from = now; // Look for napping Nodes & propose removing from Cloud for (H2ONode h2o : cloud._memary) { long delta = now - h2o._last_heard_from; if (delta > SUSPECT) { // We suspect this Node has taken a dirt nap if (!h2o._announcedLostContact) { Paxos.print("hart: announce suspect node", cloud._memary, h2o.toString()); h2o._announcedLostContact = true; } } else if (h2o._announcedLostContact) { Paxos.print("hart: regained contact with node", cloud._memary, h2o.toString()); h2o._announcedLostContact = false; } } counter++; } }
private static void checkOS() throws Exception { Integer cpus = (Integer) server.getAttribute(os, "AvailableProcessors"); System.out.println("# CPUs = " + cpus); Long vmem = (Long) server.getAttribute(os, "CommittedVirtualMemorySize"); System.out.println("Committed virtual memory = " + vmem); }