OrientDB client CPU goes over 100% with ODB2.2.20 - client

I want to load lots of data into Orient DB with multiple threads.
I'm using OrientDB 2.2.20 and Java 1.8.0_131 to run below sample test client.
But when I run this client with 5 threads and 10000 samples then the client's CPU usage goes over 100% and the process becomes almost dead.
Actually I wanted to use graph APIs to create huge number of vertices and edges between them.
But I read in some post that for massive inserts use document API and set the in & out pointers using doc APIs. Hence tried this program.
Could someone point what is wrong in the code?
public OrientDBTestClient(){
db = new ODatabaseDocumentTx(url).open(userName, password);
}
public static void main(String[] args) throws Exception{
int threadCnt = Integer.parseInt(args[0]);
OrientDBTestClient client = new OrientDBTestClient();
try {
db.declareIntent(new OIntentMassiveInsert());
Thread[] threads = new Thread[threadCnt];
for (int i = 0; i < threadCnt; i++) {
Thread loadStatsThread = new Thread(client.new LoadTask(Integer.parseInt(args[1])));
loadStatsThread.setName("LoadTask" + (i + 1));
loadStatsThread.start();
threads[i] = loadStatsThread;
}
}
catch(Exception e){
e.printStackTrace();
}
}
private class LoadTask implements Runnable{
public int count = 0;
public LoadTask(int count){
this.count = count;
}
public void run(){
long start = System.currentTimeMillis();
try{
db.activateOnCurrentThread();
for(int i = 0; i < count; ++ i){
storeStatsInDB(i +"");
}
}
catch(Exception e){
log.println("Error in LoadTask : " + e.getMessage());
e.printStackTrace();
}
finally {
db.commit();
System.out.println(Thread.currentThread().getName() + " loaded: " + count + " services in: " + (System.currentTimeMillis() - start) + "ms");
}
}
}
public void storeStatsInDB(String id) throws Exception{
try{
long start = System.currentTimeMillis();
ODocument doc = db.newInstance();
doc.reset();
doc.setClassName("ServiceStatistics");
doc.field("serviceID", id);
doc.field("name", "Service=" + id);
doc.save();
}
catch(Exception e){
log.println("Exception :" + e.getMessage());
e.printStackTrace();
}
}

db instances aren't sharable between threads.
You have two choices:
create an instance for each thread
use the pool (my first choice): http://orientdb.com/docs/last/Java-Multi-Threading.html#working-with-databases
The following example is extracted from internal tests:
pool = new OPartitionedDatabasePool("remote:localshot/test", "admin", "admin");
Runnable acquirer = () -> {
ODatabaseDocumentTx db = pool.acquire();
try {
List<ODocument> res = db.query(new OSQLSynchQuery<>("SELECT * FROM OUser"));
} finally {
db.close();
}
};
//spawn 20 threads
List<CompletableFuture<Void>> futures = IntStream.range(0, 19).boxed().map(i -> CompletableFuture.runAsync(acquirer))
.collect(Collectors.toList());
futures.forEach(cf -> cf.join());`

Related

Hystrix not interrupting the long running thread

I am new to hystrix and have created below POC where it randomly selects from execution from below use-case.
zero Error code (Successful)
Non-zero error code
Runtime Exception
Infinite loop
When it chooses the 4th one, it does not interrupt the thread. If I put the Thread.sleep() for longer time then it interrupts but doesn't interrupt the infinite loop.
Can someone tell me whether I am doing any mistake in wraping the task in the HystrixCommand? How do I interrupt the long/infinitely running threads using hystrix?
public class HystrixTesting extends HystrixCommand<HystrixResult> {
private String institutionId;
private boolean isTimedOut = true;
private boolean isRunExecuted = false;
private HystrixResult hystrixResult = new HystrixResult();
private HystrixErrorMap map = HystrixErrorMap.getInstance();
String executionType;
private Logger logger = LoggerFactory.getLogger(HystrixTesting.class);
public HystrixTesting(String institutionId, HystrixResult hystrixResult) {
super(Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("HystrixTest"))
.andCommandKey(HystrixCommandKey.Factory.asKey(institutionId)));
this.hystrixResult = hystrixResult;
this.institutionId = institutionId;
}
#Override
protected HystrixResult run() throws WIFrameworkException {
isRunExecuted = true;
logger.info("Hystrix:: Execution started for InstitutionId="
+ institutionId);
HystrixTask hystrixTask = new HystrixTask();
try {
hystrixResult = hystrixTask.executeTask(institutionId);
} catch (Exception e) {
isTimedOut = false;
}
logger.info("Hystrix:: Execution finished for InstitutionId="
+ institutionId);
int errorCode = hystrixResult.getScriptExecutionStatusCode();
String errorMessage = hystrixResult.getScriptExecutionStatusMessage();
if (HystrixErrorMap.isInErrorCodeList(errorCode)) {
map.add(institutionId, new ContextInfo(errorCode, errorMessage));
isTimedOut = false;
logger.info("Hystrix:: Throwing runtime exception in hystrix command due to ErrorCode="
+ errorCode + ", InstitutionId=" + institutionId);
throw new RuntimeException();
}
if (map.get(institutionId) != null)
map.remove(institutionId);
isTimedOut = false;
return hystrixResult;
}
#Override
protected HystrixResult getFallback() {
logger.info("Hystrix:: Fallback method called, threw an Exception in run() or hystrix has already blocked the execution for InstitutionId="
+ institutionId);
if (isTimedOut && isRunExecuted) {
String errMsg = "Hystrix:: Hystrix Command Timeout Occured while executing request for InstitutionId="
+ institutionId;
logger.error(errMsg + ", InstitutionId=" + institutionId);
hystrixResult.setScriptExecutionStatus(102, errMsg);
map.add(institutionId, new ContextInfo(102, errMsg));
} else if (!isRunExecuted) {
ContextInfo staleContext = map.get(institutionId);
logger.error("Hystrix:: Hystrix blocked the execution for InstitutionId="
+ institutionId
+ " due to ErrorCode="
+ staleContext.getErrorCode()
+ ", ErrorMessage="
+ staleContext.getErrorMessage());
hystrixResult.setScriptExecutionStatus(staleContext.getErrorCode(),
"Circuit is broken for " + institutionId + " due to "
+ staleContext.getErrorMessage());
}
return hystrixResult;
}
}
**HystrixTask.java**
public class HystrixTask {
private Logger logger = LoggerFactory.getLogger(HystrixTask.class);
static List<Integer> list = new ArrayList<Integer>();
public static int zeroErrorCounter = 1;
public static int nonZeroErrorCounter = 1;
public static int runTimeErrorCounter = 1;
public static int timeOutErrorCounter = 1;
static {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
}
/**
* Provides the output randomly 1. Successful(0 Error code) 2. Non zero
* error code 3. Runtime Exception 4. Timeout
*
* #param institutionId
* #param scriptctx
* #param executionType
* #return
* #throws InterruptedException
*/
public HystrixResult executeTask(String institutionId) {
logger.info("inside executeTask() Hystrix.getCurrentThreadExecutingCommand().name():"
+ Hystrix.getCurrentThreadExecutingCommand().name());
HystrixResult result = new HystrixResult();
try {
int random = list.get(new Random().nextInt(list.size()));
switch (random) {
case 1:
result.setScriptExecutionStatus(0, "Successfully executed");
logger.info("Hystrix:: Zero Error request served:"
+ zeroErrorCounter++);
return result;
case 2:
result.setScriptExecutionStatus(101, "Layout changed");
logger.info("Hystrix:: Non Zero Error request served:"
+ nonZeroErrorCounter++);
return result;
case 3:
logger.info("Hystrix:: Run time Error request served:"
+ runTimeErrorCounter++);
int error = 10 / 0;
case 4:
result.setScriptExecutionStatus(1000, "Timeout exception");
logger.info("Hystrix:: Timeout Error request started for:"
+ timeOutErrorCounter++);
try {
Thread.sleep(101000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("Hystrix:: Timeout Error request served:"
+ timeOutErrorCounter);
return result;
default:
logger.info("Hystrix:: default:");
return null;
}
} catch (Exception e) {
logger.info("inside exception Hystrix.getCurrentThreadExecutingCommand().name():"
+ Hystrix.getCurrentThreadExecutingCommand().name());
result.setScriptExecutionStatus(1001, "Run time exception");
logger.info("Hystrix:: Cautht: " + runTimeErrorCounter);
return result;
}
}
}

Performance of Spring boot project when makes it installed

I made two programs. One is a Client program that sends UDP Packet to UDP Server and the other is a Server program that receives UDP Packet from UDP Client.
UDP Server is made by Spring boot.
The problem is that there is difference of performance between run in STS(Spring Tools Suite) and run as jar(maven installed).
When I run this program in STS(Run as->Spring boot app), it gets all packets from client. If Client sent 3,000 packets, it gets 3,000 packet. But when I run as jar(maven install), it gets a little of packets about 200-300. Even It's exactly same code.
I don't know why it occurs. Is there any way to build spring boot app or need some option?
Whole code is too large. so I upload connect, send, bind code.
This is Client program
public UdpClient(String[] args) {
connector = new NioDatagramConnector();
connector.setHandler(this);
ConnectFuture connFuture = connector.connect(new InetSocketAddress(
args[0], 6001));
connFuture.awaitUninterruptibly();
if(args[1] != null) {
totalCount = Integer.parseInt(args[1]);
}
if(args[2] != null) {
count = Integer.parseInt(args[2]);
}
connFuture.addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
session = future.getSession();
try {
sendData(totalCount, count);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
LOGGER.error("Not connected...exiting");
}
}
});
}
private void sendData(int totalCount, int count) throws InterruptedException {
int tempCount = 0;
for (int i = 0; i < totalCount; i++) {
try{
File udpData = new File("udp_data.txt");
BufferedReader br = new BufferedReader(new FileReader(udpData));
String line;
int k = 0;
ArrayList<IoBuffer> bufList = new ArrayList<IoBuffer>();
while((line = br.readLine()) != null) {
String[] str = line.split(" ");
IoBuffer buf = IoBuffer.allocate(str.length);
for (int j = 0; j < str.length; j++) {
int hexData = Integer.parseInt(str[j],16);
byte hexByte = (byte)hexData;
buf.put(hexByte);
}
bufList.add(buf);
buf.flip();
}
long startTime = System.currentTimeMillis();
for (int j = 0; j < count; j++) {
session.write(bufList.get(j));
tempCount++;
if(j % 100 == 0) {
Thread.sleep(1);
}
}
long endTime = System.currentTimeMillis();
System.out.println("oper Time : " + (endTime - startTime));
} catch(Exception e) {
e.printStackTrace();
}
}
System.out.println("Total Send Count : " + tempCount);
}
and this is server program.
#Service
public class ModBusUdpMina {
private static Logger logger = LoggerFactory.getLogger(ModBusUdpMina.class);
#Autowired
private IoHandlerAdapter ioUdpHandlerAdapter;
#Autowired
private ProtocolCodecFactory ambProtocolCodecFactory;
#PostConstruct
public void bind() {
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
acceptor.getFilterChain().addLast(\"codec\", new ProtocolCodecFilter(ambProtocolCodecFactory));
acceptor.setHandler(ioUdpHandlerAdapter);
acceptor.getSessionConfig().setReuseAddress(true);
acceptor.getSessionConfig().setReceiveBufferSize(1024*512);
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
try {
acceptor.bind(new InetSocketAddress(6001));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Performance test:singleton class with and without double check locking

I have two implementations of singleton classes
public class Test2 {
private static Test2 _instance=new Test2();
private Test2(){
}
public static synchronized Test2 getInstance(){
if(_instance == null){
_instance = new Test2();
}
return _instance;
}
}
And:
public class TestSingleton {
private static TestSingleton _instance=new TestSingleton();
private TestSingleton(){
}
public static TestSingleton getInstance(){
if (_instance == null) {
synchronized (TestSingleton.class) {
if (_instance == null) {
_instance = new TestSingleton();
}
}
}
return _instance;
}
I want to parametrize my finding in terms of time taken, what I did is this:
Callable<Long> task = new Callable<Long>() {
#Override
public Long call() throws Exception {
long start = System.nanoTime();
**TestSingleton.getInstance();**
long end = System.nanoTime();
return end - start;
}
};
for (int i = 0; i < 100000; i++) {
futList.add(es1.submit(task));
}
for (Future<Long> fut : futList) {
try {
totalTime1.getAndAdd(fut.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Time taken by S1 " + totalTime1.get());
.
.
ExecutorService es2 = Executors.newFixedThreadPool(threadpool);
Callable<Long> task1 = new Callable<Long>() {
#Override
public Long call() throws Exception {
long start = System.nanoTime();
Test2.getInstance();
long end = System.nanoTime();
return end - start;
}
};
for (int i = 0; i < 100000; i++) {
futList1.add(es2.submit(task1));
}
for (Future<Long> fut : futList1) {
try {
totalTime2.getAndAdd(fut.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Time taken by S2 " + totalTime2.get());
The results I got is:
Time taken by S1 4636498
Time taken by S2 5127865
First question is this the correct approach? and second even if I comment the getinstances method in both the call(), I get different times of execution of two identical blocks:
Time taken by S1 1506640
Time taken by S2 2156172
Don't measure each execution and sum the times, there will be too much inaccuracy in the individual measurements. Instead, get start time, execute 100000 times, get end time. Also, execute a few 1000 times before you start measuring to avoid skewing by start-up costs.

java null pointer from another class

I have 2 classes one called Gui and one called Listener.
The code for Listener is :
package app.assignment.u0961036.core;
import net.jini.space.JavaSpace;
public class Listener extends Thread {
private JavaSpace space;
private Gui list;
public void run() {
space = SpaceUtils.getSpace("localhost");
System.out.println("In Listener");
int i = 0;
while(true){
i++;
try{
Message mTemplate = new Message();
System.out.println("Listner: template created");
Message nextMessage = (Message)space.take(mTemplate,null,Long.MAX_VALUE);
System.out.println("Listner: Message created");
String message = nextMessage.message;
System.out.println("Listner: message= "+message);
list.newMessage(message);
} catch ( Exception e) {
e.printStackTrace();
}
if(i % 10 == 0){
System.out.println("I = "+i);
}
}
}
public static void listen() {
(new Listener()).start();
}
The relevant code in Gui is:
public void newMessage(String message){
System.out.println("in new message");
chatTextArea.append(" Someone Says: " + message + "\n" );
}
When The code in Listener is run I get a null pointer from the following line:
list.newMessage(message);
I'm not sure why because the object is created.
The GUI is also created win the Gui class if you haven't already guessed.
any Ideas?
package app.assignment.u0961036.core;
import net.jini.space.JavaSpace;
public class Listener extends Thread {
private JavaSpace space;
space = new JavaSpace();
private Gui list;
list = new Gui();
public void run() {
space = SpaceUtils.getSpace("localhost");
System.out.println("In Listener");
int i = 0;
while(true){
i++;
try{
Message mTemplate = new Message();
System.out.println("Listner: template created");
Message nextMessage = (Message)space.take(mTemplate,null,Long.MAX_VALUE);
System.out.println("Listner: Message created");
String message = nextMessage.message;
System.out.println("Listner: message= "+message);
list.newMessage(message);
} catch ( Exception e) {
e.printStackTrace();
}
if(i % 10 == 0){
System.out.println("I = "+i);
}
}
}
public static void listen() {
(new Listener()).start();
}
The code above should fix your problem I think. In Java you create Objects by declaring and instantiating and initialising them. Read here for more information.

How do I get to load image in J2ME?

I am using TimerTask and ImageLoader class to load n image to an image item.
public class Imageloader implements Runnable{
private ImageItem item=null;
private String url=null;
/*
* initializes the imageItem
*/
public Imageloader(ImageItem item,String url){
this.item=item;
this.url=url;
}
private Image getImage(String url) throws IOException {
item.setLabel(item.getLabel()+12);
System.out.println("Test 5");
HttpConnection connection = null;
DataInputStream inp = null;
int length;
byte[] data;
try {System.out.println("Test 6");
connection = (HttpConnection) Connector.open(url);
item.setLabel(item.getLabel()+13);
connection.getResponseMessage();
System.out.println("Test 7");
length = (int) connection.getLength();
item.setLabel(item.getLabel()+14);
System.out.println("Length is "+length);
System.out.println("Test 8");
data = new byte[length];
inp = new DataInputStream(connection.openInputStream());
item.setLabel(item.getLabel()+15);
System.out.println("Test 9");
inp.readFully(data);
item.setLabel(item.getLabel()+16);
System.out.println("Test 10");
return Image.createImage(data, 0, data.length);
}
finally {
if (connection != null) connection.close();
if (inp != null)inp.close();
}
}
public void run() {
System.out.println("Test 1");
Image image=null;
try{
if (url!=null){
System.out.println("Test 2");
image=getImage(url);
System.out.println("Test 3");
item.setImage(image);
item.setLabel(item.getLabel()+17);
System.out.println("Test 4");
}
else{
item.setAltText("Map address specified is incorrect");
}
}
catch(IOException e){
item.setAltText("Map cannot be loaded now");
}
}
}
public class MapTimer extends TimerTask{
DatagramConnection connection=null;
String message=null;
Imageloader imageretriever;
ImageItem item=null;
MapTimer (DatagramConnection connection,String message,ImageItem Img_map ){
this.connection=connection;
this.message=message;
this.item=Img_map;
item.setLabel(item.getLabel()+1);
System.out.println("Map is initizlized...");
}
public void run() {
System.out.println("Map starting...");
item.setLabel(item.getLabel()+2);
String serverquery=null;
try {
item.setLabel(item.getLabel()+3);
sendMessage(message);
item.setLabel(item.getLabel()+4);
//serverquery=receiveMessage() ;
item.setLabel(item.getLabel()+5);
//item.setLabel(" Loading...." );
//formatmessage(serverquery);
String url="http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap"+
"&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318"+
"&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false";
Imageloader Im=new Imageloader(item,url);
item.setLabel(item.getLabel()+6);
(new Thread(Im)).start();
item.setLabel(item.getLabel()+7);
System.out.println("server map query is::: "+serverquery);
} catch (IOException ex) {
System.out.println("Error2"+ex);
}
catch(Exception e){
System.out.println("Error3"+e);
}
}
/*
* Sends a message via UDP to server
*/
private void sendMessage(String message) throws IOException{
Datagram packet_send=null;
if (connection != null){
byte[] packetsenddata=message.getBytes();
packet_send = connection.newDatagram(packetsenddata,packetsenddata.length);
connection.send(packet_send);
packet_send = null;
}
}
}
This is how I set the Timer;
MapTimer maptimer=new MapTimer (connection,mapquery ,Img_map );
Timer timer=new Timer();
timer.schedule(maptimer, 5000, 100000);
It's working fine with the enulator but as I deploy it on my mob,the image is not loading..
The image label is somewhat like Stopping 234567234567 which implies that my timer is running fine. It is not entering the ImageLoader class... How do get to load this image?
This is difficult to say without further debuggind. I recommend you to use Micrologger + a web server, in order to debug your midlets on the device.
Looking to your code, I suspect of this line length = (int) connection.getLength();. Could it fail on Nokia's IO library implementation?

Resources