Why JDK's JConsole seek virtual machines using both jvmstat and attach api? - jvm

I'm writing some JVM instance related application and looking at open source to see how it's solve some problems. The JConsole from JDK7 collects running VMs in two ways (look at source licensed by GPL2 in jdk/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java). The first is jvmstat way, code like this:
private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) {
MonitoredHost host;
Set vms;
try {
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
vms = host.activeVms();
} catch (java.net.URISyntaxException sx) {
throw new InternalError(sx.getMessage());
} catch (MonitorException mx) {
throw new InternalError(mx.getMessage());
}
for (Object vmid: vms) {
if (vmid instanceof Integer) {
int pid = ((Integer) vmid).intValue();
String name = vmid.toString(); // default to pid if name not available
boolean attachable = false;
String address = null;
try {
MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
// use the command line as the display name
name = MonitoredVmUtil.commandLine(mvm);
attachable = MonitoredVmUtil.isAttachable(mvm);
address = ConnectorAddressLink.importFrom(pid);
mvm.detach();
} catch (Exception x) {
// ignore
}
map.put((Integer) vmid,
new LocalVirtualMachine(pid, name, attachable, address));
}
}
}
Second is attach way, and looks like this:
private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
List<VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vms) {
try {
Integer vmid = Integer.valueOf(vmd.id());
if (!map.containsKey(vmid)) {
boolean attachable = false;
String address = null;
try {
VirtualMachine vm = VirtualMachine.attach(vmd);
attachable = true;
Properties agentProps = vm.getAgentProperties();
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
vm.detach();
} catch (AttachNotSupportedException x) {
// not attachable
} catch (IOException x) {
// ignore
}
map.put(vmid, new LocalVirtualMachine(vmid.intValue(),
vmd.displayName(),
attachable,
address));
}
} catch (NumberFormatException e) {
// do not support vmid different than pid
}
}
}
My question: why it uses two different tools for retrieving virtual machines list? I know that through attach api you can list VMs running by this same JRE only, but jvmstat can give you list of all VMs running with any JRE versions. I have tested JRE/JDK 7 32 and 64-bit only because theys and newer are my target, sadly on windows only. Is not suffiecient to use jvmstat only? Is there any case when some VM is visible by attach api, but jvmstat can't see it?

Usually, for finding all jvm process, attach api is equal to jvmstat. But in some customized circumstances, it's diffrent. And the diffrent is com.sun.tools.attach.spi.AttachProvider.
e.g. in Windows platform, jvmstat finds java process by listing all files in the directory %TEMP%/hsperfdata_caoxudong(In linux, it is /tmp/hsperfdata_caoxudong). And attach api finds java processes by AttachProvider instance. jdk provides a default an AttachProvider implementation, which depends on your OS platform. In Windows Platform, the implementation is sun.tools.attach.WindowsAttachProvider. In its listVirtualMachines method, if isTempPathSecure method return false, it will iterate all processes, and find all processes, which loaded library "jvm.dll". You can install your own AttachProvider implementation to find java processes with your own ways, and the result may be diffrent with jvmstat.
The installation of AttachProvider is here.

Related

Disable JMX/MBeans in JVM

I have two Debezium SQL Server connectors that have to connect to one database and publish two different tables. Their name and database.history.kafka.topic are unique. Still when adding the second one (using a POST request) I get below exceptions. I don't want to use a unique value for database.server.name which counterintuitively has been used for metric name.
java.lang.RuntimeException: Unable to register the MBean 'debezium.sql_server:type=connector-metrics,context=schema-history,server=mydatabase'
Caused by: javax.management.InstanceAlreadyExistsException: debezium.sql_server:type=connector-metrics,context=schema-history,server=mydatabase
We won't be using JMX/MBeans so it's okay to disable it, but the question is how. If there is a common way to do it for JVM please advise.
I even see below code in Debezium where it registers a MBean. Looking just at two first lines, it seems one way to bypass this issue is forcing ManagementFactory.getPlatformMBeanServer() to return null. So another way of asking the same question may be how to force ManagementFactory.getPlatformMBeanServer() return null?
public synchronized void register() {
try {
final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
if (mBeanServer == null) {
LOGGER.info("JMX not supported, bean '{}' not registered", name);
return;
}
// During connector restarts it is possible that Kafka Connect does not manage
// the lifecycle perfectly. In that case it is possible the old metric MBean is still present.
// There will be multiple attempts executed to register new MBean.
for (int attempt = 1; attempt <= REGISTRATION_RETRIES; attempt++) {
try {
mBeanServer.registerMBean(this, name);
break;
}
catch (InstanceAlreadyExistsException e) {
if (attempt < REGISTRATION_RETRIES) {
LOGGER.warn(
"Unable to register metrics as an old set with the same name exists, retrying in {} (attempt {} out of {})",
REGISTRATION_RETRY_DELAY, attempt, REGISTRATION_RETRIES);
final Metronome metronome = Metronome.sleeper(REGISTRATION_RETRY_DELAY, Clock.system());
metronome.pause();
}
else {
LOGGER.error("Failed to register metrics MBean, metrics will not be available");
}
}
}
// If the old metrics MBean is present then the connector will try to unregister it
// upon shutdown.
registered = true;
}
catch (JMException | InterruptedException e) {
throw new RuntimeException("Unable to register the MBean '" + name + "'", e);
}
}
You should use a single Debezium SQL Server connector for this, and use the table.include.list property on the connector to list the two tables you want to capture.
https://debezium.io/documentation/reference/stable/connectors/sqlserver.html#sqlserver-property-table-include-list

In dotnet core how can I ensure only one copy of my application is running?

In the past I have done something like this
private static bool AlreadyRunning()
{
var processes = Process.GetProcesses();
var currentProc = Process.GetCurrentProcess();
logger.Info($"Current proccess: {currentProc.ProcessName}");
foreach (var process in processes)
{
if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id)
{
logger.Info($"Another instance of this process is already running: {process.Id}");
return true;
}
}
return false;
}
Which has worked well. In the new dotnet core world everything has a process name of dotnet so I can only run one dotnet app at a time! Not quite what I want :D
Is there an ideal way of doing this in dotnet? I see mutex suggested but I am not sure I understand the possible downsides or error states running on other systems than a windows machine.
.NET Core now supports global named mutex. From PR description, that added that functionality:
On systems that support thread process-shared robust recursive mutexes, they will be used
On other systems, file locks are used. File locks, unfortunately, don't have a timeout in the blocking wait call, and I didn't find any other sync object with a timed wait with the necessary properties, so polling is done for timed waits.
Also, there is a useful note in Named mutex not supported on Unix issue about mutex name, that should be used:
By default, names have session scope and sessions are more granular on Unix (each terminal gets its own session). Try adding a "Global" prefix to the name minus the quotes.
In the end I used a mutex and it seeeeeems okay.
I grabbed the code from here
What is a good pattern for using a Global Mutex in C#?
The version of core I am using does not seem to have some of the security settings stuff so I just deleted it. I am sure it will be fine. (new Mutex only takes 3 parameters)
private static void Main(string[] args)
{
LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
logger = LogManager.GetLogger("console");
logger.Info("Trying to start");
const string mutexId = #"Global\{{guid-guid-guid-guid-guid}}";
bool createdNew;
using (var mutex = new Mutex(false, mutexId, out createdNew))
{
var hasHandle = false;
try
{
try
{
hasHandle = mutex.WaitOne(5000, false);
if (!hasHandle)
{
logger.Error("Timeout waiting for exclusive access");
throw new TimeoutException("Timeout waiting for exclusive access");
}
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
// Perform your work here.
PerformWorkHere();
}
finally
{
if (hasHandle)
{
mutex.ReleaseMutex();
}
}
}
}

Opendaylight: how to get MAC address of switch from datapath ID?

I am developing an application for opendaylight Carbon where I need to know the MAC address of the switch. Can I determine this from the DpnId when the switch connects? Thanks.
Not sure which MAC you are referring to. If you are referring MAC address of each ofport of the DPN then you can register listener for FlowCapableNodeConnector model and you can get MAC by calling FlowCapableNodeConnector#getHardwareAddress in add method of listener. And if you are talking about VM/packet Source/destination MAC, then you first you need to punt the packet to controller and then you can use PacketProcessingListener and extract MAC as shown below.
public void onPacketReceived(PacketReceived notification) {
final short tableId = notification.getTableId().getValue();
final byte[] data = notification.getPayload();
Ethernet res = new Ethernet();
try {
res.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte);
} catch (Exception e) {
LOG.warn("PacketInHandler: Failed to decode Packet ", e);
return;
}
try {
Packet pkt = res.getPayload();
LOG.info("Packet type is ->{}", pkt.getClass().getName());
if (pkt instanceof IPv4) {
IPv4 ipv4 = (IPv4) pkt;
byte[] srcMac = res.getSourceMACAddress();
byte[] dstMac = res.getDestinationMACAddress();
}
}
}
The DPID uniquely identifies the switch. The MAC address is generally not exposed.
Moreover, the switch itself generally does not have a MAC address
(they may have tens of MAC addresses for different functions/interfaces).
Switches work at a lower level, though, they work with MAC addresses.

usbManager openDevice call fails after several hundred successful attempts

I'm using usbmanager class to manage USB host on my android 4.1.1 machine.
all seems to work quite well for a few hundreds of transactions until (after ~ 900 transactions) opening the device fails, returning null without exception.
Using a profiler it doesn't seem to be a matter of memory leakage.
this is how I initialize the communication from my main activity (doing this once):
public class MainTestActivity extends Activity {
private BroadcastReceiver m_UsbReceiver = null;
private PendingIntent mPermissionIntent = null;
UsbManager m_manager=null;
DeviceFactory m_factory = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
m_UsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
// call your method that cleans up and closes communication with the device
Log.v("BroadcastReceiver", "Device Detached");
}
}
}
};
registerReceiver(m_UsbReceiver, filter);
m_manager = (UsbManager) getSystemService(Context.USB_SERVICE);
m_factory = new DeviceFactory(this,mPermissionIntent);
}
and this is the code of my test:
ArrayList<DeviceInterface> devList = m_factory.getDevicesList();
if ( devList.size() > 0){
DeviceInterface devIf = devList.get(0);
UsbDeviceConnection connection;
try
{
connection = m_manager.openDevice(m_device);
}
catch (Exception e)
{
return null;
}
The test will work OK for 900 to 1000 calls and after this the following call will return null (without exception):
UsbDeviceConnection connection;
try
{
connection = m_manager.openDevice(m_device);
}
You might just run out of file handles, a typical limit would be 1024 open files per process.
Try calling close() on the UsbDeviceConnection, see doc.
The UsbDeviceConnection object has allocated system ressources - e.g. a file descriptor - which will be released only on garbage collection in your code. But in this case you run out of ressources before you run out of memory - which means the garbage collector is not invoked yet.
I had opendevice fail on repeated runs on android 4.0 even though I open only once in my code. I had some exit paths that did not close the resources and I had assumed the OS would free it on process termination.
However there seems to be some issue with release of resources on process termination -I used to have issues even when I terminated and launched a fresh process.
I finally ensured release of resources on exit and made the problem go away.

Can Not find static text control when the test is executed on virtual machine

I am using Microsoft Automation UI framework to develop my automation test cases. The problem I faced with is related to interaction with static text control. I am just trying to get the control's text. The test work perfect when I run the test on my local machine. The problem is when I run the test via Test Controler on the (no matter which) Test Agent. The error which appear is that the static control text can not be found.
Theis is the part of my code where I am trying to initialize the control I want to interact with:
private void Init(TreeScope treeScope, params Condition[] properties)
{
try
{
List<Condition> propertiesList = properties.ToList();
propertiesList.Add(Condition.TrueCondition);
bool controlFound = Wait.ForCondition(
() =>
{
try
{
TestControl = Parent.FindFirst(treeScope,
new System.Windows.Automation.AndCondition(propertiesList.ToArray()));
return !TestControl.Current.IsOffscreen;
}
catch
{
return false;
}
});
if (!controlFound)
{
throw new ElementNotAvailableException(DescriptiveName + "Control is NOT found");
}
this.GetItAsUITestControl().WaitForControlReady(Playback.PlaybackSettings.WaitForReadyTimeout);
if (TestControl.Current.IsKeyboardFocusable)
{
TestControl.SetFocus();
}
string controlFullName = this.TestControl.Current.ControlType.ProgrammaticName;
DescriptiveName = "< " + DescriptiveName + " " + controlFullName.Substring(controlFullName.LastIndexOf(".")) + " >";
}
catch (ElementNotAvailableException ex)
{
Report.Error(ex.Message);
}
catch (Exception ex)
{
Report.Error(ex.Message);
}
}
Any ideas?
I am using Microsoft System Center Virtual Machine Manager 2008 R2 for managing my virtual machines (I think all machines are vmware). But from my prespective the problem is not in the virtual machine because all of the tests are executed without any problems on the VM except the one which verify the Static Text Control content. I am 100% sure that the desctop of the VM where the tests are executed is active because I am able to look at it using VMWare Remote Console.
In terms of execution of the tests on the remote machine I am using Test Controlers and Test Agents which comes with Visual Studio.