Run time time out exception on couch base client - java

While updating(Upsert) the document we have been getting the time out exception.
couch base server version: 3.0.3 enterprise edition
couch base client - 2.1.2
Configuration:
connectTimeout: 10000
viewTimeout: 75000
queryTimeout: 75000
Setting:
// Create couchbase cluster client
CouchbaseEnvironment couchEnv = DefaultCouchbaseEnvironment.builder()
.connectTimeout(configuration.getCouchbase().getConnectTimeout()) //10000ms = 10s, default is 5s
.viewTimeout(configuration.getCouchbase().getViewTimeout())
.queryTimeout(configuration.getCouchbase().getQueryTimeout())
.autoreleaseAfter(5000)
.build();
cluster = CouchbaseCluster.create(couchEnv,configuration.getCouchbase().getHosts());
bucket = cluster.openBucket(configuration.getCouchbase().getBucket(), configuration.getCouchbase().getPassword());
Code:
public UserDocument updateUserDocument(UserDocument userDocument)
throws Exception {
userDocument.setLastUpdatedTime(Calendar.getInstance().getTime());
JsonObject userDocObject = JsonObject.fromJson(gson
.toJson(userDocument));
JsonDocument userDocumentJson = JsonDocument.create(
String.valueOf(userDocument.getUserId()), userDocObject);
//Getting Timeout Exception here
JsonDocument responseDoc = bucket.upsert(userDocumentJson);
// update device mappings in redis
if (userDocument.getUserDevices() != null && userDocument.getUserDevices().size() > 0) {
for (UserDevice userDevice : userDocument.getUserDevices())
{
redisClientService.putDeviceMappingInCache( userDevice.getDeviceId(), userDocument.getPartnerId(), userDocument);
}
}
return gson.fromJson(responseDoc.content() != null ? responseDoc.content().toString() : null, UserDocument.class);
}
Error:
ERROR [2015-07-28 12:16:59,120] com.personagraph.dropwizard.resource.UserManagementResource: Internal Error in gettting user details
! java.util.concurrent.TimeoutException: null
! Causing: java.lang.RuntimeException: java.util.concurrent.TimeoutException
! at com.couchbase.client.java.util.Blocking.blockForSingle(Blocking.java:93) ~[pgweb-usermanagement-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at com.couchbase.client.java.view.DefaultViewRow.document(DefaultViewRow.java:44) ~[pgweb-usermanagement-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at com.couchbase.client.java.view.DefaultViewRow.document(DefaultViewRow.java:39) ~[pgweb-usermanagement-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Can anyone please let me know if I need any setting or code changes

Related

Getting 400 as status code in cosmo DB batch Operation

I am trying to store multiple types of Records in Cosmo DB using batch operation. But I am getting 400 status in CosmoBatchResponse object and errorMessage is null. Internally , one item is giving 400 , all the other operations have a 424 status code (failed dependency). From this [document] https://learn.microsoft.com/en-us/rest/api/cosmos-db/http-status-codes-for-cosmosdb I can see there could be many reason of 400 , but if errorMessage is null then how to find what went wrong . Also , same msg is getting stored via create call ,facing issue while batch save only.
PartitionKey partitionKey = new PartitionKey("customerNo");
CosmosBatch batch = CosmosBatch.createCosmosBatch(partitionKey);
batch.createItemOperation(customer);
I have tried to store via create method only looping on CosmosItemOperation and it is getting stored.
CosmosBatchResponse response=paymentRepository.createBatch(cosmosBatch);
for(CosmosItemOperation itemOp:cosmosBatch.getOperations()) {
System.out.println(paymentRepository.create(itemOp.getItem(),""));// Here it is getting stored.
}
public CosmosBatchResponse createBatch(CosmosBatch cosmosBatch) {
CosmosBatchResponse response = null;
try {
response = container.executeCosmosBatch(cosmosBatch);
System.out.println(response.isSuccessStatusCode()); -- returns false
System.out.println(response.getErrorMessage()); -- returns null
return response;
} catch (final Exception e) {
int statusCode = CosmosUtils.getCosmosStatusCode(e);
if (CONFLICT_RESOURCE == statusCode) {
log.error(
"CosmosCreateDocumentException: Resource already exists for Document : {}",
response.getErrorMessage());
}
shouldRetryOnException(e);
log.error(
"CosmosCreateDocumentException for Document {} - {}, {}", cosmosBatch, e.getMessage(), e);
throw new GenericRepositoryException(e.getMessage(), e);
}
}

Scala - Cassandra: cluster read fails with error "Can't use this Cluster instance because it was previously closed"

I'm getting this error when reading from a table in a 5 node cluster using datastax drivers.
2015-02-19 03:24:09,908 ERROR [akka.actor.default-dispatcher-9] OneForOneStrategy akka://user/HealthServiceChecker-49e686b9-e189-48e3-9aeb-a574c875a8ab Can't use this Cluster instance because it was previously closed
java.lang.IllegalStateException: Can't use this Cluster instance because it was previously closed
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1128) ~[cassandra-driver-core-2.0.4.jar:na]
at com.datastax.driver.core.Cluster.init(Cluster.java:149) ~[cassandra-driver-core-2.0.4.jar:na]
at com.datastax.driver.core.Cluster.connect(Cluster.java:225) ~[cassandra-driver-core-2.0.4.jar:na]
at com.datastax.driver.core.Cluster.connect(Cluster.java:258) ~[cassandra-driver-core-2.0.4.jar:na]
I am able to connect using cqlsh and perform read operations.
Any clue what could be the problem here?
settings:
Consistency Level: ONE
keyspace replication strategy:
'class': 'NetworkTopologyStrategy',
'DC2': '1',
'DC1': '1'
cassandra version: 2.0.6
The code managing cassandra sessions is central and it is;
trait ConfigCassandraCluster
extends CassandraCluster
{
def cassandraConf: CassandraConfig
lazy val port = cassandraConf.port
lazy val host = cassandraConf.host
lazy val cluster: Cluster =
Cluster.builder()
.addContactPoints(host)
.withReconnectionPolicy(new ExponentialReconnectionPolicy(100, 30000))
.withPort(port)
.withSocketOptions(new SocketOptions().setKeepAlive(true))
.build()
lazy val keyspace = cassandraConf.keyspace
private lazy val casSession = cluster.connect(keyspace)
val session = new SessionProvider(casSession)
}
class SessionProvider(casSession: => Session) extends Logging {
var lastSuccessful: Long = 0
var firstSuccessful: Long = -1
def apply[T](fn: Session => T): T = {
val result = retry(fn, 15)
if(firstSuccessful < 0)
firstSuccessful = System.currentTimeMillis()
lastSuccessful = System.currentTimeMillis()
result
}
private def retry[T](fn: Session => T, remainingAttempts: Int): T = {
//retry logic
}
The problem is, cluster.connect(keyspace) will close the cluster itself if it experiences NoHostAvailableException. Due to that during retry logic, you are experiencing IllegalStateException.
Have a look at Cluster init() method and you will understand more.
The solution for your problem would be, in the retry logic, do Cluster.builder.addContactPoint(node).build.connect(keyspace). This will enable to have a new cluster object while you retry.
Search your code for session.close().
You are closing your connection somewhere as stated in the comments. Once a session is closed, it can't be used again. Instead of closing connections, pool them to allow for re-use.

elasticsearch fail with error "Failed to execute phase [query_fetch], all shards failed"

When i try to index data and then do query, all is good, but if i start my app and will do query without indexing before it i get that error
Exception in thread "main" org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query_fetch], all shards failed at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:272)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:224)
at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteFetch(SearchServiceTransportAction.java:307)
at org.elasticsearch.action.search.type.TransportSearchQueryAndFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryAndFetchAction.java:71)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:216)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:203)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:186)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Here is my code and settings of elasticsearch
// settings
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
settings.put("client.transport.sniff", false);
settings.put("path.home", "/path/to/elastic/home");
settings.put("index.number_of_replicas", 0);
settings.put("index.number_of_shards", 1);
settings.put("action.write_consistency", "one");
settings.build();
// creating of node and client
NodeBuilder nb = new NodeBuilder().settings(settings).local(true).data(true);
Node node = nb.node();
Client client = node.client();
/*
for (int i = 0; i <= 15; i++) {
IndexResponse response = client.prepareIndex("twitter2", "tweet2", String.valueOf(i))
.setSource(json1)
.execute()
.actionGet();
}
*/
searchRequestBuilder = client.prepareSearch("twitter2")
.setTypes("tweet2")
.setQuery(QueryBuilders.matchQuery("user", "user0"))
.setQuery(QueryBuilders.matchQuery("message","message1"))
.setExplain(true)
.setSearchType(SearchType.DFS_QUERY_AND_FETCH).setSize(200);
SearchResponse searchRespons = searchRequestBuilder.execute().actionGet(); // here is error
What is wrong in me settings?
Can you try the same after adding a sleep for say 10 seconds after
Client client = node.client();
I feel Elasticsearch haven't recovered the shards before you have hit the search request.
Even the admin call to "wait for yellow" should work for you

Java Play framework parsing JSON error

I want to setup Janrain authentication to my Play! project which is hosted on GAE and uses GAE module. But I get the following error while I try to login:
RuntimeException occured : Cannot parse JSON (check logs)
And Play highlighs the following line as error:
JsonElement rpxJson = rpxRequest.get().getJson();
Here is method that I use for token callback:
public static void tokenCallback(String token) {
Properties p = Play.configuration;
// Try the driver
String rpxApi = p.getProperty("login.rpx.apiKey");
WSRequest rpxRequest = WS.url("http://rpxnow.com/api/v2/auth_info");
// get RPX
rpxRequest.setParameter("token", token);
rpxRequest.setParameter("apiKey", rpxApi);
JsonElement rpxJson = rpxRequest.get().getJson();
JsonElement profile = rpxJson.getAsJsonObject().get("profile");
String identifier = profile.getAsJsonObject().getAsJsonPrimitive("identifier").getAsString();
welcome(identifier);
}
And here is the error that I get from terminal:
Internal Server Error (500) for request POST /login/tokencallback
Execution exception (In /app/controllers/Login.java around line 27)
RuntimeException occured : Cannot parse JSON (check logs)
play.exceptions.JavaExecutionException: Cannot parse JSON (check logs)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.RuntimeException: Cannot parse JSON (check logs)
at play.libs.WS$HttpResponse.getJson(WS.java:668)
at controllers.Login.tokenCallback(Login.java:27)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
... 1 more
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at play.libs.WS$HttpResponse.getJson(WS.java:665)
... 7 more
Caused by: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:390)
at com.google.gson.JsonParser.parse(JsonParser.java:60)
... 9 more
What can I do? Please, help me to solve this problem.
Thanks in advance.
OK, Here is my first suggestion. Try using the HTTPS connection for the URL. I ran into some problems with the HTTP connection. Here is how I do the Janrain connection:
WSRequest rpxRequest = WS.url("https://rpxnow.com/api/v2/auth_info");
// get RPX
rpxRequest.setParameter("token", token);
rpxRequest.setParameter("apiKey", rpxApi);
HttpResponse res = null;
try {
res = rpxRequest.post();
} catch (JavaExecutionException ex) {
Log.error("unknown error ", ex);
Validation.addError("", "Unknown Error: please try again");
Validation.keep();
Secure.login();
} catch (Exception ex) {
Log.error("Most likely SSL error", ex);
Validation.addError("", "SSL Error: please try again");
Validation.keep();
Secure.login();
}
if (res.getStatus() != 200) {
Log.error("status 200 error");
Validation.addError("", "Status 200 error: please try again");
Validation.keep();
Secure.login();
}
JsonElement rpxJson = res.getJson();
JsonElement profile = rpxJson.getAsJsonObject().get("profile");
JsonObject profileJson = profile.getAsJsonObject();
Having called the URL http://rpxnow.com/api/v2/auth_info , it immediately redirects to https://rpxnow.com/api/v2/auth_info (http s ). I suspect you don't get the JSON answer, but a http redirect code in your call to the web service.
Two possibilites:
1) Change the web service call to https://rpxnow.com/api/v2/auth_info , this probably solves your problem, failing that;
2) Change the line JsonElement rpxJson = rpxRequest.get().getJson(); into something like
HttpResponse httpResponse = rpxRequest.get();
Logger.log ( httpResponse.getString() );
if ( httpResponse.success() ) {
JsonElement rpxJson = httpResponse.getJson();
} else {
// fail gracefully
}
and report back on the contents of the answer which gets logged in the second line.

How to get error-response packet from apple's Enhanced notification format with javapns

ResponsePacket theErrorResponse = pushedNotification.getResponse();I'm using javapns2.2 and trying to catch error-response packet from from apple's Enhanced notification format. When I send push notification (with invalid token for example) the code
Exception theProblem = pushedNotification.getException();
theProblem.printStackTrace();
outputs some error to the console, but
ResponsePacket theErrorResponse = pushedNotification.getResponse();
if (theErrorResponse != null && theErrorResponse.isErrorResponsePacket()) {
System.out.println(theErrorResponse.getMessage());
System.out.println(theErrorResponse.getStatus());
}
always returns null. How can I obtain status codes with getResponse()?
Here is a part of my code:
List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, devices);
for (PushedNotification pushedNotification : notifications) {
if(pushedNotification.isSuccessful())
{
System.out.println(pushedNotification.getDevice().getToken());
}
else
{
System.out.println(pushedNotification.getDevice().getToken());
Exception theProblem = pushedNotification.getException();
theProblem.printStackTrace();
ResponsePacket theErrorResponse = pushedNotification.getResponse();
if (theErrorResponse != null && theErrorResponse.isErrorResponsePacket()) {
System.out.println(theErrorResponse.getMessage());
System.out.println(theErrorResponse.getStatus());
}
}
}
Thank you for your help
I figured it out.
getResponse() returns null because Exception mechanism is preventing the call to APNs to be more efficient. True errors are packed within Exception mechanism.
Everything is super explained at this link:
http://code.google.com/p/javapns/issues/detail?id=79&can=1

Categories