when exception is thrown in my controller, action's are getting executed again and again, when error is thrown this cycle is going on.
try
{
LogManager.Info("Call:" + invocation.TargetType.Name + "::" + invocation.Method.Name);
var startTime = DateTime.Now;
invocation.Proceed();
var endTime = DateTime.Now;
LogManager.Info("CallEnd:" + invocation.TargetType.Name + "::" + invocation.Method.Name + " Execution Time(Milliseconds): " + (endTime - startTime).Milliseconds);
}
catch (Exception ex)
{
//HttpContext.Current.Request.Abort();
var builder = new StringBuilder();
var dataSource = ConfigurationManager.ConnectionStrings["dbMain"].ToString().Split(';')[0];
builder.AppendLine();
builder.AppendLine(string.Concat(Enumerable.Repeat(">", 50)));
builder.AppendLine("Server Time:-" + DateTime.Now);
//builder.AppendLine("Requested Url:" + HttpContext.Current.Request.Url);
builder.AppendLine(dataSource);
builder.AppendLine("Error at " + invocation.TargetType.Name + "::" + invocation.Method.Name + "(" + JsonConvert.SerializeObject(invocation.Arguments, Formatting.Indented) + ")");
builder.AppendLine(string.Concat(Enumerable.Repeat("-", 50)));
builder.AppendLine(JsonConvert.SerializeObject(ex, Formatting.Indented).Replace("\\n", System.Environment.NewLine));
builder.AppendLine(string.Concat(Enumerable.Repeat("<", 50)));
LogManager.Error(builder.ToString());
LogManager.SendMail(ex, builder.ToString());
throw;
}
If you want the exception to stop bubbling up, don't re-throw it.
In your code there is the :
"throw ;" line. Remove it.
Related
public void insertdata()
{
try
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customers(customer_name,address,email,mob_no) values('" + tname.Text + "','" + taddress.Text + "','" + temail.Text + "','" + tmobno.Text + "')";
cmd.ExecuteNonQuery();
MessageBox.Show("Registered Successfully");
con.Close();
tname.Text = "";
taddress.Text = "";
temail.Text = "";
tmobno.Text = "";
}
catch (Exception e)
{
MessageBox.Show("Problem in Registering Customer,Please Enter all Fields Correctly" + e);
}
}
''This is is function which i want to call in another Form which is advancePayment on button_click.
As you want to call the same function at multiple places, what you can do is create another class with insertdata() function in it. After that, you can add reference of that class in your respective forms.
public double getMinCBQta(long enteCod, long artCod) throws FailureException {
logger.debug("getMinCBqta() - ENTER");
double minCBQta = 0;
final TypedQuery<Double> query = em.createQuery("select NVL(Min(c.qtaCapienza),0) " + "from CapienzaBanco c "
+ "where c.id.enteCod = :ENTECOD AND c.id.artCod = :ARTCOD "
+ "AND c.id.nrPromo = 0 AND c.id.annoPromo = 0 AND c.qtaCapienza!= 0 ", Double.class);
try {
query.setParameter(CommonConstants.ENTECOD, enteCod);
query.setParameter(CommonConstants.ARTCOD, artCod);
minCBQta = query.getSingleResult();
} catch (final NoResultException ne) {
logger.warn("getMinCBQta :No result received from CAPIENZA_BANCO with enetCod " + enteCod + "artCod: " + artCod);
} catch (final Exception e) {
logger.error("getMinCBQta : Error Retriving data from CAPIENZA_BANCO " + e.getMessage());
throw new FailureException("getMinCBQta : Error Retriving data from CAPIENZA_BANCO " + e);
}
logger.debug("getMinCBqta()" + " EXIT");
return minCBQta;
}
When I execute the code I developed to call an Async method of the linq2Twitter, I am getting a System.Aggregate Exception, the code is below:
static async Task<List<myTwitterStatus>> getRetweets(ulong tweetID, TwitterContext twitterCtx)
{
List<myTwitterStatus> reTweets = new List<myTwitterStatus>();
var publicTweets =
await
(from tweet in twitterCtx.Status
where tweet.Type == StatusType.Retweets &&
tweet.ID == tweetID
select tweet)
.ToListAsync();
if (publicTweets != null)
publicTweets.ForEach(tweet =>
{
if (tweet != null && tweet.User != null)
{
myTwitterStatus tempStatus = new myTwitterStatus();
tempStatus.Country = tweet.Place.Country;
tempStatus.createdAt = tweet.CreatedAt;
tempStatus.FavoriteCount = long.Parse(tweet.FavoriteCount.ToString());
tempStatus.ID = tweet.ID;
tempStatus.isTruncated = tweet.Truncated;
tempStatus.Lang = tweet.Lang;
tempStatus.MaxID = tweet.MaxID;
tempStatus.PlaceFullname = tweet.Place.FullName;
tempStatus.RetweetCount = tweet.RetweetCount;
tempStatus.ScreenName = tweet.ScreenName;
tempStatus.Text = tweet.Text;
tempStatus.UserFriends = tweet.User.FriendsCount;
tempStatus.UserCreated = tweet.User.CreatedAt;
tempStatus.UserFollowers = tweet.User.FollowersCount;
tempStatus.UserFavorities = tweet.User.FavoritesCount;
tempStatus.UserFriends = tweet.User.FriendsCount;
tempStatus.UserLocation = tweet.User.Location;
tempStatus.UserName = tweet.User.Name;
tempStatus.UserTweetCount = tweet.User.StatusesCount;
reTweets.Add(tempStatus);
}
});
return reTweets;
}
The issue is when I called the method
var authorizer = new SingleUserAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = SM.Default.Consumer_key2.ToString(),
ConsumerSecret = SM.Default.Consumer_secret2.ToString(),
OAuthToken = SM.Default.Access_token2.ToString(),
OAuthTokenSecret = SM.Default.Access_secret2.ToString()
}
};
TwitterContext twitterCtx = new TwitterContext(authorizer);
Task<List<myTwitterStatus>> task = Task<List<myTwitterStatus>>.Factory.StartNew(() => getRetweets(ulong.Parse(tweet.StringId), twitterCtx).Result);
task.Wait();
List<myTwitterStatus> tempList = task.Result.ToList<myTwitterStatus>();
foreach (var ret in tempList)
{
un = file.RemoveSpecialCharacters(ret.UserName);
sn = file.RemoveSpecialCharacters(ret.ScreenName);
tweets.AppendLine(account + "," + getWE(ret.createdAt) + "," + Text + "," + un + "," + sn + "," + ret.createdAt + "," +
file.RemoveSpecialCharacters(ret.UserLocation) + ",,,1,," + ret.UserTweetCount + "," +
ret.RetweetCount + "," + ret.FavoriteCount + "," + ret.UserFollowers);
I would appreciate any kind of assistance about it, I have no idea how to solve it.
Can you add a try/catch so see what the inner exception is.
try
{
//Perform your operation here
}
catch (AggregateException aex)
{
//Dive into inner exception
}
Thanks to all for the help, I activate the breakpoints for the specific exception and an additional validation was added, the issue was not in the async process, it was because it was trying to handle a null as an string, to hendle this error this line was modified sn = file.RemoveSpecialCharacters(ret.ScreenName ?? string.Empty);
The full code is below:
private void getRetweets(TwitterStatus tweet)
{
Text = file.RemoveSpecialCharacters(tweet.Text);
tweetID = tweet.StringId;
#region Get retweets
RetweetsOptions retOpt = new RetweetsOptions();
if (int.Parse(tweet.RetweetCountString.ToString()) > 1)
retOpt.Count = int.Parse(tweet.RetweetCountString.ToString()) + 1;
else
retOpt.Count = 1;
String errorText = "";
DateTime fromDate, toDate;
if (radDate.Checked == true)
{
fromDate = dtpFrom.Value;
toDate = dtpTo.Value;
}
else
{
fromDate = tweet.CreatedDate;
toDate = DateTime.Now;
}
TwitterResponse<TwitterStatusCollection> retweet = null;
if (int.Parse(tweet.RetweetCountString.ToString()) > 100)
{
var authorizer = new SingleUserAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = SM.Default.Consumer_key2.ToString(),
ConsumerSecret = SM.Default.Consumer_secret2.ToString(),
OAuthToken = SM.Default.Access_token2.ToString(),
OAuthTokenSecret = SM.Default.Access_secret2.ToString()
}
};
TwitterContext twitterCtx = new TwitterContext(authorizer);
//HELPER: https://www.youtube.com/watch?v=IONqMWGn9-w
try
{
Task<List<myTwitterStatus>> task = null;
Parallel.Invoke(
() =>
{
task = getRetweets(ulong.Parse(tweet.StringId), twitterCtx);
while (!task.IsCompleted)
{
Thread.Sleep(250);
}
});
List<myTwitterStatus> tempList = task.Result.ToList<myTwitterStatus>();
foreach (var ret in tempList)
{
un = file.RemoveSpecialCharacters(ret.UserName);
sn = file.RemoveSpecialCharacters(ret.ScreenName ?? string.Empty);
tweets.AppendLine(account + "," + getWE(ret.createdAt) + "," + Text + "," + un + "," + sn + "," + ret.createdAt + "," +
file.RemoveSpecialCharacters(ret.UserLocation) + ",,,1,," + ret.UserTweetCount + "," +
ret.RetweetCount + "," + ret.FavoriteCount + "," + ret.UserFollowers);
}
}catch(Exception ex){
Console.WriteLine("Error: " + ex.Message+ Environment.NewLine + ex.StackTrace);
}
return;
}
else
retweet= TwitterStatus.Retweets(tokensRet, Decimal.Parse(tweet.Id.ToString()), retOpt);
if (retweet.Result.ToString() == "Success" && retweet.ResponseObject.Count > 0 && retweet!=null)
{
int retPages = int.Parse(tweet.RetweetCountString.ToString()) + 1 / 20;
for (int page = 0; page <= retPages; page++)
{
try
{
//List<TwitterStatus> retList = new List<TwitterStatus>(retweet.ResponseObject.Page = page);
retweet.ResponseObject.Page = page;
List<TwitterStatus> retList = retweet.ResponseObject.ToList<TwitterStatus>();
foreach (var ret in retList)
{
try
{
if (ret.CreatedDate.CompareTo(fromDate) >= 0 && ret.CreatedDate.CompareTo(toDate) <= 0)
{
#region Get UN Sync
getUN(ret, ref un, ref sn);
#endregion
tweets.AppendLine(account + "," + getWE(ret.CreatedDate) + "," + Text + "," + un + "," + sn + "," + ret.CreatedDate + "," +
file.RemoveSpecialCharacters(ret.User.Location.ToString()) + ",,,1,," + ret.User.NumberOfStatuses.ToString() + "," +
ret.RetweetCount + "," + ret.User.NumberOfFavorites.ToString() + "," + ret.User.NumberOfFollowers.ToString());
}
else if (tweet.CreatedDate.CompareTo(toDate) <= 0)//if the tweet's created date is lower than the from's date, exits the loop
break;
}
catch (NullReferenceException ex)
{
errorText = ex.Source + Environment.NewLine + ex.StackTrace;
continue;
}
}
}
catch (Exception ex)
{
errorText = ex.Source + Environment.NewLine + ex.StackTrace;
//MessageBox.Show(ex.Message + Environment.NewLine + "Rate Limit was reached!" + Environment.NewLine +
// "Wait an hour or try a shorter date range", "Rate Limit Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else if (error == false && retweet.Result.ToString() != "Success")
{
errorText = retweet.Result.ToString();
MessageBox.Show("Retweets: Something went wrong!" + Environment.NewLine + errorText, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
error = true;
}
#endregion
}
I am using the following code to connect my .net client (CQL based) to 3 node Cassandra cluster. I am getting the data (from RabbitMQ) 30 records/sec and they get stored in cassandra upto 800-900 rows smoothly. But after that i am getting this follwing exception. Can anyone please tell me what are the optimization/changes i can make to avoid this exception. I could't find specific solution to this problem anywhere.
Error: ERROR ErrorLog - error in Cassandra GetCWCRow Function Connection :None of the hosts tried for query are available (tried: X.X.X.201:9042, X.X.X.200:9042, X.X.X.X:9042)
Code :
using Cassandra;
using Consumer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace RabbitMqCarWaleUserTracking
{
class DataAccessCassandra
{
public bool InsertCookieLogData(string cwc, string page_uri)
{
try
{
Logs.WriteInfoLog("Cassandra InsertCookieLogData a Function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
string pageCategory = string.Empty;
try
{
if ((Regex.IsMatch(page_uri, "/newcars/upcomingcars", RegexOptions.IgnoreCase)))
{
pageCategory = "upcomingCars";
}
else
if ((Regex.IsMatch(page_uri, "/newcars/dealers/newCarDealerShowroom", RegexOptions.IgnoreCase)) || (Regex.IsMatch(page_uri, "/newcars/dealers/listnewcardealersbycity", RegexOptions.IgnoreCase)
|| (Regex.IsMatch(page_uri, "/newcars/dealers/dealerdetails", RegexOptions.IgnoreCase))))
{
pageCategory = "newcarsDealers";
}
else
if ((Regex.IsMatch(page_uri, "/offers", RegexOptions.IgnoreCase)) || (Regex.IsMatch(page_uri, "/alloffers", RegexOptions.IgnoreCase)))
{
pageCategory = "offers";
}
else
if ((Regex.IsMatch(page_uri, "/dealer/testdrive", RegexOptions.IgnoreCase)))
{
pageCategory = "dealerTestDrive";
}
if (pageCategory != string.Empty)
{
Row result = session.Execute("select logdate from pageWiseCookieLog where cwc ='" + cwc + "' and page_uri ='" + pageCategory + "' and logdate= '" + DateTime.Today.ToString("yyyy-MM-dd") + "'").FirstOrDefault();
if (result == null)
{
session.Execute("insert into pageWiseCookieLog (cwc, page_uri, logdate) values ('" + cwc + "' , '" + pageCategory + "' , '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
session.Execute("insert into pageWiseCookieLogByld (cwc, page_uri, logdate) values ('" + cwc + "' , '" + pageCategory + "' , '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
session.Dispose();
cluster.Dispose();
return true;
}
}
else
{
//don't want to store the data for rest of the page category but need to return true
session.Dispose();
cluster.Dispose();
return true;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCookieLogData function with cwc :" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCookieLogData function connection :" + ex.Message);
SendMail.HandleException(ex, subject);
}
return false;
}
public string GetCWCRow(string cwc, int index, string mobileId)
{
try
{
Logs.WriteInfoLog("Cassandra GetCWCRow Function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
try
{
Row result = session.Execute("select cur_visit_id from usertracking where cwc ='" + cwc + "'").FirstOrDefault();
if (result != null)
{
session.Dispose();
cluster.Dispose();
return result[0].ToString();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra GetCWCRow function with cwc :" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra GetCWCRow Function Connection :" + ex.Message);
SendMail.HandleException(ex, subject);
}
return string.Empty;
}
public bool InsertCWCRecords(string cwv, Cut_Case caseType, int index, string mobileId)
{
try
{
Logs.WriteInfoLog("Cassandra InsertCWCRecords function called for case:" + caseType);
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
try
{
bool _isProcessed = false;
string visitCount = "";
string[] leadParameters = cwv.Split('.');
string cwc = leadParameters[0];
string visitId = leadParameters[1];
string visitStartTime = leadParameters[2];
string visitPrevPageTime = leadParameters[3];
string visitLastPageTime = leadParameters[4];
if (leadParameters.Length == 6)
{
visitCount = leadParameters[5];
}
string TOT_TIME_SPENT = (Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime)).ToString();
if ((int)caseType == 1) //to enter new cwc data in summary table
{
session.Execute("insert into usertracking (cwc, cur_visit_id, cur_visit_last_ts, tot_page_view, tot_time_spent, tot_visit_count, cur_visit_datetime) values ('" + cwc + "' , '" + visitId + "' ," + visitStartTime + "," + "1" + "," + TOT_TIME_SPENT + "," + "1" + ", '" + DateTime.Today.ToString("yyyy-MM-dd") + "' )");
_isProcessed = true;
}
if ((int)caseType == 2) //if cwc exits and visit id is same
{
Row result = session.Execute("select tot_page_view, tot_time_spent from usertracking where cwc ='" + cwc + "'").FirstOrDefault();
int page_cnt_val = int.Parse(result[0].ToString()) + 1;
Int64 time_spt_val = Int64.Parse(result[1].ToString()) + Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime);
session.Execute("update usertracking SET cur_visit_last_ts = " + visitLastPageTime + ", tot_page_view = " + page_cnt_val + ", tot_time_spent = " + time_spt_val + " WHERE cwc = '" + cwc.Trim() + "'");
_isProcessed = true;
}
if ((int)caseType == 3) //if cwc exits ans visit id is different
{
Row result = session.Execute("select tot_page_view, tot_time_spent, tot_visit_count, cur_visit_last_ts, cur_visit_datetime from usertracking where cwc = '" + cwc + "'").First();
int page_cnt_val = int.Parse(result[0].ToString()) + 1;
Int64 time_spt_val = Int64.Parse(result[1].ToString()) + Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime);
int visit_val = int.Parse(result[2].ToString()) + 1;
Int64 prev_visit_ts_val = Int64.Parse(result[3].ToString());
String prev_visit_datetime_val = Convert.ToDateTime(result[4].ToString()).ToString("yyyy-MM-dd");
session.Execute("update usertracking SET cur_visit_id = '" + visitId + "' , tot_visit_count= " + visit_val
+ " , prev_visit_last_ts= " + prev_visit_ts_val + ", prev_visit_datetime = '" + prev_visit_datetime_val
+ "' , cur_visit_last_ts = " + visitLastPageTime
+ ", tot_page_view = " + page_cnt_val + ", tot_time_spent = " + time_spt_val
+ ", cur_visit_datetime='" + DateTime.Today.ToString("yyyy-MM-dd")
+ "' WHERE cwc = '" + cwc.Trim() + "'");
_isProcessed = true;
}
session.Dispose();
cluster.Dispose();
return _isProcessed;
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCWCRecords function with cwv :" + cwv + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
return false;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCWCRecords function connection :" + ex.Message);
SendMail.HandleException(ex, subject);
return false;
}
}
public bool UpdateReferrerTimeSpent(string cwc, int referrerCategoryId, double referrerTimeSpent, int index, string mobileId)
{
bool _isUpdated = false;
try
{
Logs.WriteInfoLog("Cassandra UpdateReferrerTimeSpent function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect("cw");
try
{
Row result = session.Execute("select time_spent_in_sec from userTimeSpentPage WHERE cwc = '" + cwc.Trim() + "' And logdate = '" + DateTime.Today.ToString("yyyy-MM-dd") + "' And page_category_id =" + referrerCategoryId).FirstOrDefault();
if (result != null)
{
if (result[0].ToString().Trim() != string.Empty)
{
Int64 page_time_spent_val = Int64.Parse(result[0].ToString());
Int64 tot_time_spt_val = page_time_spent_val + Int64.Parse(referrerTimeSpent.ToString());
session.Execute("update userTimeSpentPage set time_spent_in_sec= " + tot_time_spt_val + "WHERE cwc = '" + cwc.Trim() + "' And logdate = '" + DateTime.Today.ToString("yyyy-MM-dd") + "' And page_category_id=" + referrerCategoryId);
}
}
else
{
session.Execute("insert into userTimeSpentPage (cwc, page_category_id, time_spent_in_sec, logdate) values ('" + cwc + "' ," + referrerCategoryId + "," + referrerTimeSpent + ", '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
}
_isUpdated = true;
session.Dispose();
cluster.Dispose();
return _isUpdated;
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra UpdateReferrerTimeSpent function with cwc:" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
return _isUpdated;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra UpdateReferrerTimeSpent function connection" + ex.Message);
SendMail.HandleException(ex, subject);
return _isUpdated;
}
}
}
}
Edited Question:
output of netstat -an | awk '/^tcp/ {print $NF}' | sort | uniq -c | sort -rn
On Machine 1 While cassandra running :
773 ESTABLISHED
36 LISTEN
1 CLOSE_WAIT
After cassandra stopped :
274 ESTABLISHED
36 LISTEN
1 CLOSE_WAIT
Machine 2 while cassandra running :
3941 ESTABLISHED
26 LISTEN
7 CLOSE_WAIT
After cassandra stopped :
26 LISTEN
9 ESTABLISHED
On machine 3 while cassandra running :
500 ESTABLISHED
21 LISTEN
After cassandra stopped :
21 LISTEN
13 ESTABLISHED
The NoHostAvailableException can be thrown for many reasons. However this is all about the problem described in the driver documentation:
Exception thrown when a query cannot be performed because no host are
available. This exception is thrown if
either there is no host live in
the cluster at the moment of the query
all host that have been tried
have failed due to a connection problem
Now why is this happening - there could be several reasons for this.
Possibility of simultaneous major Garbage collection on all 3 nodes. I personally don't think it is the case, but you should definitely read on this and see if it may apply to your case. Here is a link to a very nice documednt describing how to tune GC in Cassandra. The fact that you create cluster and session objects practically for any call instead of storing them as singletons and just reusing them, may make things even worse.
After looking at the function that throws an error, I am more or less convinced that the problem is that after so many inserts, your nodes just timeout while reading the wide row during this statement: Row result = session.Execute("select cur_visit_id from usertracking where cwc ='" + cwc + "'").FirstOrDefault();. Further down the road you're performing a lot of updates for the same clustering key cws which, due to immutable nature of the SSTables, makes a lot of versioned data, resulting in added data retrieval time, since the cluster needs to combine all of this data for each your request.
It is hard to make recommendations without any table schema, and reverse engineering won't help much either, but I would recommend somehow utilizing a composite primary key for faster lookups. Tune your JVMs, and make session and cluster singletons and reuse them in your code. See if this helps.
Read through the Cassandra cluster logs, focusing on the times around when the issues happen. See if any clues are in these logs, like Garbage collection activity, or timeout errors.
HTH
Roman
I am pretty new to nodejs.
What I want my code to do it to query a database a number of times, collect data from all the queries in one variable and then use it somewhere.
But I guess nodejs instead of waiting for the result of the queries, execute without blocking.
This is what I think is happening. Sorry if I am wrong.
for (var i = step_min; i < (step_max); i += step) {
query = 'select count(' + colName + ') as num_count from ' +
rows[0].tablename + ' where ' + 'dictionaryid=' +
rows[0].dictionaryid + ' and ' + colName + ' between ' + i +
' and ' + (i + step);
connection.query(query, function(err, rows1, fields) {
if (err) {
console.log(err);
throw err;
}
try {
console.log("$$$$$$$$$$$$$$$ pushed : "+ rows1[0].num_count);
contents.push(rows1[0].num_count);
console.log('contents : '+ contents+'\n');
} catch (e) {
if (e instanceof SyntaxError) {
console.log("Syntax Error for input function");
}
}
});
console.log("##################### " + query + "\n");
console.log('contents : '+ contents+'\n');
}
Any advice either on how to block nodejs till the result from query is obtained or otherwise a way to restructure my code ?
Thanks in advance.
You are correct that it is not waiting for your queries before executing. You can look at the node-mysql-queues module which you can use to queue queries, and execute the given callback after all have executed (it will also allow you do perform transactions)
Another (hack) approach is to set a counter of the number of queries you are executing. In the callback of each transaction save the result into your return object and decrement the counter. If it is <= 0 then all your queries have completed, and you can execute your main callback with the return object.
Also, watch out for SQL injection.
try this:
https://github.com/luciotato/waitfor
your code with wait.for:
for (var i = step_min; i < (step_max); i += step) {
query = 'select count(' + colName + ') as num_count from ' +
rows[0].tablename + ' where ' + 'dictionaryid=' +
rows[0].dictionaryid + ' and ' + colName + ' between ' + i +
' and ' + (i + step);
var rows1 = wait.forMethod(connection,"query",query); //waits until callback
console.log("$$$$$$$$$$$$$$$ pushed : "+ rows1[0].num_count);
contents.push(rows1[0].num_count);
}
console.log("##################### " + query + "\n");
console.log('contents : '+ contents+'\n');
} catch (e) {
if (e instanceof SyntaxError) {
console.log("Syntax Error for input function");
}
....
}
Doing something like below could be a work around.
//Its just a test code whatever came to mind first so I'll tune it later...
var contents = [];
var lock = 0;
for (var i = step_min; i < (step_max + step); i += step) {
lock++;
}
for (var i = step_min; i < (step_max + step); i += step) {
query = 'select count(' + colName + ') as num_count from ' + rows[0].tablename + ' where ' + 'dictionaryid=' + rows[0].dictionaryid + ' and ' + colName + ' between ' + i + ' and ' + (i + step);
connection.query(query, function(err, rows1, fields) {
if (err) {
console.log(err);
throw err;
}
try {
console.log("$$$$$$$$$$$$$$$ pushed : " + rows1[0].num_count);
contents.push(rows1[0].num_count);
console.log('contents : ' + contents + '\n');
} catch (e) {
if (e instanceof SyntaxError) {
console.log("Syntax Error for input function");
}
}
lock--;
if (lock == 0) {
queryDone();
}
});
}
function queryDone() {
console.log("##################### " + query + "\n");
console.log('contents : ' + contents + '\n');
var id = obj.exptID + '.' + obj.exptITR + '.' + obj.nodeID + '.' + obj.resourceName + '.' + colName;
serie = {
name: colName,
data: contents,
id: id
};
console.log("--------------------\n " + step_max + "\n" + step_min + "\n------------------------\n");
}
The approach is to fire a function when all queries are done as Nick said...