datastax java driver - why cassandra cluster write is ok but update always fail (about 30%)? -
i'm working cassandra cluster 4 nodes (amazon), have test_ks_r2 replication factor = 2, write , update consistency level = all, read consistency level = one. code using datastax java driver (version 2.1.5):
// schema of user column family create table user ( id text primary key, password text, username text ); // resultset class import java.util.arraylist; import java.util.list; import java.util.objects; import java.util.hashmap; import java.util.map; public class resultset { private list<string> idlist = new arraylist<string>(); private map<string, list<string>> data = new hashmap<string, list<string>>(); public void setidlist(list<string> v) {idlist = v;} public list<string> getidlist() { return idlist;} public void setdata(map<string, list<string>> v) {data = v;} public map<string, list<string>> getdata() {return data;} } // test cassandra class import java.util.arraylist; import java.util.list; import java.util.objects; import java.util.hashmap; import java.util.map; import com.datastax.driver.core.*; import com.datastax.driver.core.cluster.builder; import com.datastax.driver.core.querybuilder.querybuilder; class testcassandra { private consistencylevel getconsistencylevel(string consistency) { system.out.println("consistency level string: " + consistency); if(consistency.equals("one")) { system.out.println("using consistency level: one"); return consistencylevel.one; } else if(consistency.equals("two")) { system.out.println("using consistency level: two"); return consistencylevel.two; } else if(consistency.equals("quorum")) { system.out.println("using consistency level: quorum"); return consistencylevel.quorum; } else if(consistency.equals("all")) { system.out.println("using consistency level: all"); return consistencylevel.all; } else { system.out.println("using default consistency level: one"); return consistencylevel.one; } } public void testwriteupdateread(int n, string mode, int delay) { if (mode.equals("querybuilder")) { system.out.println("test write & read querybuilder ..."); } else if (mode.equals("preparedstatement")) { system.out.println("test write & read preparedstatement ..."); } else { system.exit(-1); } resultset resultset = write(n, mode, getconsistencylevel("all")); read(resultset.getidlist(), resultset.getdata(), getconsistencylevel("one")); // update data update(resultset, mode, getconsistencylevel("all"), delay); // read again read(resultset.getidlist(), resultset.getdata(), getconsistencylevel("one")); } public resultset update(resultset resultset, string mode, consistencylevel consistencylevel, int delay) { preparedstatement ps = session.prepare("update user set password = ? id = ?") .setconsistencylevel(consistencylevel); list<string> idlist = resultset.getidlist(); map<string, list<string>> data = resultset.getdata(); (string idread: idlist) { try { thread.sleep(delay); } catch (exception e) { system.out.println(e); } string newpassword = "new password" + idread; if (mode.equals("querybuilder")) { session.execute( querybuilder.update("user") .where(querybuilder.eq("id", idread)) .with(querybuilder.set("password", newpassword)) .setconsistencylevel(consistencylevel) ); } else { resultset result = session.execute(ps.bind(newpassword, idread)); system.out.println(result); } list<string> itemvalues = new arraylist<string>(); itemvalues.add("empty"); itemvalues.add(newpassword); data.put(idread, itemvalues); } system.out.println("update " + objects.tostring(idlist.size(), null) + " times"); resultset.setdata(data); return resultset; } public resultset write(int n, string mode, consistencylevel consistencylevel) { preparedstatement ps = session .prepare("insert user (id, username, password) values (?, ?, ?)") .setconsistencylevel(consistencylevel); string idwrite = ""; list<string> idlist = new arraylist<string>(); map<string, list<string>> data = new hashmap<string, list<string>>(); (int i=0; i<n;i++) { idwrite = objects.tostring(system.currenttimemillis(),null); string username = "username" + idwrite; string password = "password" + idwrite; list<string> itemvalues = new arraylist<string>(); itemvalues.add(username); itemvalues.add(password); if (mode.equals("querybuilder")) { session.execute( querybuilder.insertinto("user") .value("id", idwrite) .value("username", username) .value("password", password) .setconsistencylevel(consistencylevel) ); } else { session.execute(ps.bind(idwrite, username, password)); } idlist.add(idwrite); data.put(idwrite, itemvalues); } resultset resultset = new resultset(); resultset.setidlist(idlist); resultset.setdata(data); system.out.println("write " + objects.tostring(n, null) + " times"); return resultset; } public void read(list<string> idlist, map<string, list<string>> data, consistencylevel consistencylevel) { int readcount; int success = 0; (string idread: idlist) { preparedstatement stmt = session.prepare("select * user id = ?").setconsistencylevel(consistencylevel); list<string> itemvalues = data.get(idread); try { readcount = 0; row resultrow = null; (row row : session.execute(stmt.bind(idread))) { readcount++; resultrow = row; } // there should 1 read, in such case, successful read of recent write if (readcount==1) { if(itemvalues.get(1).equals(resultrow.getstring("password"))) { success++; } else { system.out.println("password: " + itemvalues.get(1)); system.out.println("password in database: " + resultrow.getstring("password")); } } } catch (exception e) { success = success + 0; // not increase success since there exception } } system.out.println("successfully read " + objects.tostring(success, null) + " times"); } }
here result
write 40 times consistency level string: 1 using consistency level: 1 read 40 times consistency level string: using consistency level: update 40 times consistency level string: 1 using consistency level: 1 read 32 times
update fail ~ 30%. doing wrong?
Comments
Post a Comment