I'm writing some Java code using MongoDB with Java API and I'm unsure of some part of the Javadoc.
In a multi-thread context I use DBCollection.html#update(com.mongodb.DBObject, com.mongodb.DBObject) to update a unique document, but I saw that two threads could try to write concurrently. In this context, I observed that only one write was done, as Mongodb seems to use optimistic write lock, but I wanted to find out programmatically in which thread the write was the one who wrote, and which one was not. As a "no update" behavior was silent (I mean no exception or something), I searched into the API some way to answer my issue and after some tests found out this method: WriteResult#getN()
public int getN()
Gets the "n" field
Returns:
The description is, hum... not really exhaustive. My tests showed that the thread that win the write has a getN() that return 1, and the other 0.
So my question is: Could someone confirm this ?
From the GetLastError() documentation
The return value from the command is an object with various fields. The common fields are listed below; there may also be other fields.
ok - true indicates the getLastError command completed successfully. This does NOT indicate there wasn't a last error.
err - if non-null, indicates an error occurred. Value is a textual description of the error.
code - if set, indicates the error code which occurred. connectionId - the id of the connection
lastOp - the op-id from the last operation
For updates:
n - if an update was done, this is the number of documents updated.
So in this context, 'get "n" field' means get n which is the number of documents updated. Without "multi" being set to true it can only be either 0 or 1.
Related
Quick question regarding how to build a visual on a specific condition of a java counter in Grafana please.
Currently, I have a small piece of java code, straightforward.
private String question(MeterRegistry meterRegistry) {
if (someCondition()) {
Counter.builder("theCounter").tags("GOOD", "GOOD").register(meterRegistry).increment();
return "good";
} else {
LOGGER.warn("it is failing, we should increment failure");
Counter.builder("theCounter").tags("FAIL", "FAIL").register(meterRegistry).increment();
return "fail";
}
}
As you can see, it is very simple, just a "if a condition is met, increment the GOOD counter, if not, increment the FAIL counter"
I am interested in building a dashboard for the failures only.
When I query my /prometheus endpoint I successfully see:
myCounter_total{FAIL="FAIL",} 7.0
myCounter_total{GOOD="GOOD",} 3.0
Hence, I started using this query.
myCounter_total{_ws_="workspace",_ns_="namespace",_source_="source}
Unfortunately, this query is giving me the visual for everything, the GOOD and the FAIL. In my example, I see all 10 counters, while I just want to see the 7 failures.
I tried putting
myCounter_total{FAIL="FAIL",_ws_="workspace",_ns_="namespace",_source_="source}
{{FAIL}}
But no luck.
May I ask what did I miss please?
Create only one counter for this case and give it a label named status, for example. Now, depending on whether the good or the fail condition occurs, increment your counter with a string "GOOD" or "FAIL" as a value for the status label, for example like in this pseudo code:
Counter myCounter =
Counter.build().name("myCounter").help("This is my counter").labelNames("status").register();
if (someCondition()) {
myCounter.labels("GOOD").increment();
} else {
myCounter.labels("FAIL").increment();
}
Now you should be able see a query output like this:
myCounter_total{status="FAIL"} 7.0
myCounter_total{status="GOOD"} 3.0
For visualization purposes, if you'd like to see two graphs, one for good and one for bad cases, you could use something like below. This one query querying myCounter_total reveals its data and the Legend takes care of separating values/graphs with different status label values.
Query: myCounter_total
Legend: {{status}}
I'm trying to get CPU and memory usage with SNMP. I have a Java code which takes the oid and runs the "get" command. I can reach the values with MIB-II. However when i import HOST-RESOURCES-MIB in the code i can't get CPU information it returns Null. But some oids work properly in HOST-RESOURCES-MIB.
For example:
hrSystemUpTime(.1.3.6.1.2.1.25.1.1.0) gives me the value 3:51:15.07
hrProcessorLoad(.1.3.6.1.2.1.25.3.3.1.2.0) gives me the value Null
What is the problem?
I've solved the problem. In MIBs the information kept in indexes. Thus, in order to reach a particular information, you need to know which index it's kept.So, i did SNMPWalk on .1.3.6.1.2.1.25.3.3.1.2 for hrProcessorLoad (omit zero) and i got the values. We can also use getNext command to reach the correct index.
I am a new bee to Automation and Java. I am working on a problem which requires me to read the read time stock market data from the database and verify it with the same with the value seen on the UI. I am ok having approximations up to 5% in the value. To verify if these tests have passed its important for me to assert the values with the value in the UI.
I have a small logic to verify these values, I wanted to know if this is a good way of coding on java or do i have a better way to achieve these results.
Alorigthm.
I read the int/float value from db.
Calculate 5% of the value in step 1.
Get the value in the UI and assert if its greater then or equal to value in step 2.
If greater i say Asseert.assertEquals(true,true) else i fail my assert.
If any better way to work for these values, request a better answer.
It's more usual to have your Assertion represent the meaning of your test, having to assert(true, true) does not do this. So:
3. Calculate the absoluete difference between the value obtained in step 1 and the UI value (when I say absolute value, you need to remember that the UI might be higher or lower than the db value, you need to make the difference to be always positive)
4. Assert.assertThat( difference < theFivePercentValue)
Also you could consider using the Hamcrest extension to JUnit that includes a closeTo() method.
Following is the sample data set that I need to group together, if you look closely they are mostly similar text lines but with very minute difference of having either a person id or ID .
Unexpected error:java.lang.RuntimeException:Data not found for person 1X99999123 . Clear set not defined . Dump
Unexpected error:java.lang.RuntimeException:Data not found for person 2X99999123 . Clear set not defined . Dump
Unexpected error:java.lang.RuntimeException:Data not found for person 31X9393912 . Clear set not defined . Dump
Unexpected error:java.lang.RuntimeException:Data not found for person 36X9393912 . Clear set not defined . Dump
Exception in thread "main" javax.crypto.BadPaddingException: ID 1 Given final block not properly padded
Exception in thread "main" javax.crypto.BadPaddingException: ID 2 Given final block not properly padded
Unexpected error:java.lang.RuntimeException:Data not found for person 5 . Clear set not defined . Dump
Unexpected error:java.lang.RuntimeException:Data not found for person 6 . Clear set not defined . Dump
Exception in thread "main" java.lang.NullPointerException at TripleDESTest.encrypt(TripleDESTest.java:18)
I want to group them so that final result is like
Unexpected error:java.lang.RuntimeException:Data not found - 6
Exception in thread "main" javax.crypto.BadPaddingException - 2
Exception in thread "main" java.lang.NullPointerException at - 1
Is there an existing API or algorithm available to handle such cases ?
Thanks in Advance.
Cheers
Shakti
The question is tagged as machine learning, so I am going to suggest classification approach.
You can tokenize each string, and use all tokens from training set as possible boolean features - an instance has the feature, if it contains this token.
Now, using this data, you can build (for instance) a C4.5 - a decision tree from the data. Make sure the tree use trimming once it is build, and minimum number of examples per leaf >1.
Once the tree is built, the "clustering" is done by the tree itself! Each leaf contains the examples which are considered similar to each other.
You can now extract this data by traversing the classification tree and extracting the samples stored in each leaf into its relevant cluster.
Notes:
This algorithm will fail for the sample data you provided because it cannot handle well if one msg is unique (the NPE in your example) - it will probably be in the same leaf as BadPaddingException.
No need to reinvent the wheel - you can use weka - an open source Machine Learning library in java, or other existing libraries for the algorithms
Instead of using the tokens as binary features, they can also be numerical features, you can use where is the token in the string, is it the 1st or 10th token?
I think that you should propably create a method that parses the text and and filters out the pattern you wish to remove... However I am not entirely sure of what you want to do....
I think that you what you want to do can probably be achieved through the StringTokenizer class...
If you know the format of the messages the easiest way is to use regular expression and count the matches.
Regular expressions are fully supported in Java and their usage is surely faster than a clustering algorithm.
I use spymemcached 2.6rc1 in my Java project where I want to use the Long class as a storable object. Unfortunately, when I store e.g. new Long(0) object, the get(...) and incr(...) give the wholly different results - get gives Long object that contains 48 value and incr gives 1.
Please note that 48 represents the ASCII "0" symbol. When I try to get the value for the same key directly from memcached (e.g. by using telnet) I get the correct result - 0. Strange, the Long is good serialized class. So, probably, there is some problem with default transcoding. Can somebody clarify how to resolve this situation?
There was an issue filed for this a while back (spymemcached bug 41). Here's what Dustin Sallings, the creator of Spymemcached said about the issue:
You can't mix IntegerTranscoder and incr/decr. incr/decr require the numbers to be
encoded as strings as they are language-agnostic server-side operations.
Here's a unit test that demonstrates what you're trying to do:
public void testIncrDecrBug41() throws Exception {
final String key="incrdecrbug41";
// set to zero
client.set(key, 86400, "0");
// retrieve it to see if it worked
assertEquals("0", client.get(key));
// increment it
assertEquals(1, client.incr(key, 1));
// fetch it again to see if it worked
assertEquals("1", client.get(key));
}
Note that the reason you get 49 is because decimal 49 is the string "1".
incr and decr cause a lot of confusion for people because of the server-side
semantics. In newer version of memcached (e.g. changes I don't have applied yet in
my binary branch), incr and decr will fail on non-numeric string values. That is,
your first incr would throw an exception.
In the future please file bugs at the Spymemcached project website. It can be found at http://code.google.com/p/spymemcached. That way we can fix them sooner.