public void testPerformInTransaction_ExistingTx() { final BaseTransaction tx1 = mock(BaseTransaction.class); TransactionFactory txFactory = mock(TransactionFactory.class); when(txFactory.createTransaction()).thenReturn(tx1); DefaultTransactionManager txManager = new DefaultTransactionManager(txFactory, mock(JdbcEventLogger.class)); final BaseTransaction tx2 = mock(BaseTransaction.class); BaseTransaction.bindThreadTransaction(tx2); try { final Object expectedResult = new Object(); Object result = txManager.performInTransaction( new TransactionalOperation<Object>() { public Object perform() { assertSame(tx2, BaseTransaction.getThreadTransaction()); return expectedResult; } }); assertSame(expectedResult, result); } finally { BaseTransaction.bindThreadTransaction(null); } }
private EpcCache getNewEpcList(String eoj) { ArrayList<EPC> epcGetter = new ArrayList<EPC>(); ArrayList<EPC> epcList = new ArrayList<EPC>(); EpcCache epcCache = new EpcCache(); try { // System.out.println(eoj); epcGetter.add(OBSERVABLE_EPC); epcGetter.add(SETABLE_EPC); epcGetter.add(GETABLE_EPC); // DataTransactionListenerDelegate delegate = new // EpcListTransactionListenerDelegate(epcCache); // DataTransactionListener listener = new DataTransactionListener(delegate); EpcListTransactionListener listener = new EpcListTransactionListener(epcCache); Transaction epcListTransaction = transactionFactory.makeMulticastTransaction(eoj, epcGetter, listener); listener.setTransaction(epcListTransaction); epcListTransaction.execute(); epcListTransaction.join(); // System.out.println(epcCache); } catch (SubnetException ex) { Logger.getLogger(ValidEpcCache.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(ValidEpcCache.class.getName()).log(Level.SEVERE, null, ex); } return epcCache; }
public void run() { int baseKey = (index * keyCount) % keys.size(); for (int i = 0; i < requests; i++) { int keyIndex = (baseKey + (i % keyCount)) % keys.size(); String key = keys.get(keyIndex); Transaction txn = fac.create(); long start = System.currentTimeMillis(); try { txn.begin(); byte[] value = txn.read(key); txn.commit(); timeElapsed += System.currentTimeMillis() - start; if (!silent.get()) { System.out.println("[Thread-" + index + "] " + key + ": " + new String(value)); } success++; } catch (Exception e) { timeElapsed += System.currentTimeMillis() - start; if (!silent.get()) { System.out.println("[Thread-" + index + "] Error: " + e.getMessage()); } failure++; } } }
public void run() { int baseKey = index; for (int i = 0; i < requests; i++) { int keyIndex = (baseKey + i); String key = keys.get(keyIndex); Transaction txn = fac.create(); long start = System.currentTimeMillis(); try { txn.begin(); String value = "random_value_" + index + "_" + i; txn.write(key, value.getBytes()); txn.commit(); timeElapsed += (System.currentTimeMillis() - start); if (!silent.get()) { System.out.println("[Thread-" + index + "] " + key + ": " + value); } success++; } catch (TransactionException e) { timeElapsed += (System.currentTimeMillis() - start); if (!silent.get()) { System.out.println("[Thread-" + index + "] Error: " + e.getMessage()); } failure++; } } }
private static void readOnlyTransaction(TransactionFactory fac, String key) { Transaction txn = fac.create(); try { txn.begin(); byte[] value = txn.read(key); System.out.println(key + ": " + new String(value)); txn.commit(); } catch (TransactionException e) { System.out.println("Error: " + e.getMessage()); } }
@NotNull @Override public Transaction buildTransaction() { return TransactionFactory.generateSingleEntryTransaction( accountProperty.get(), amountField.getDecimal(), datePicker.getValue(), memoTextField.getText(), payeeTextField.getText(), numberComboBox.getValue()); }
private static void bigLoad(TransactionFactory fac, int numKeys) { Transaction txn = fac.create(); try { txn.begin(); for (int i = 0; i < numKeys; i++) { String key = "row" + i; String value = "value" + i; txn.write(key, value.getBytes()); } txn.commit(); } catch (TransactionException e) { System.out.println("Error: " + e.getMessage()); } }
private static void blindWriteTransaction(TransactionFactory fac, String key, String value) { Transaction txn = fac.create(); try { txn.begin(); txn.write(key, value.getBytes()); if (Database.DELETE_VALUE_STRING.equals(value)) { System.out.println("Key " + key + " deleted"); } else { System.out.println(key + ": " + value); } txn.commit(); } catch (TransactionException e) { System.out.println("Error: " + e.getMessage()); } }
public T newTransaction() throws SQLException { return mTransactionFactory.startTransaction(); }
public static void main(String[] args) throws IOException { TransactionFactory fac = new TransactionFactory(); System.out.println("Welcome to MDCC Client"); System.out.println("Enter 'help' to see a list of supported commands..."); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Options options = new Options(); options.addOption("k", true, "Key of the data object"); options.addOption("v", true, "Value of the data object"); options.addOption("c", true, "Number of concurrent users to emulate"); options.addOption("n", true, "Number of transactions executed by each user"); options.addOption("t", true, "Total number of unique keys"); options.addOption("w", true, "Number of unique keys per worker"); options.addOption("s", true, "Shard ID"); options.addOption("p", true, "Primary server ID"); options.addOption("silent", false, "Enable silent mode"); CommandLineParser parser = new BasicParser(); AppServerConfiguration config = AppServerConfiguration.getConfiguration(); if (config.getAppServerUrl() != null) { System.out.println("Connecting to remote app server: " + config.getAppServerUrl()); } while (true) { CommandLine cmd; String key; String value; int concurrency = 1; int num = 1; int total = 1; int keysPerWorker = 1; System.out.print("mdcc> "); String command = reader.readLine(); String[] cmdArgs = translateCommandline(command); try { cmd = parser.parse(options, cmdArgs); } catch (ParseException e) { System.out.println("Invalid command: " + command); continue; } if (cmdArgs.length == 0) { } else if ("get".equals(cmdArgs[0])) { if (cmd.hasOption("k")) { key = cmd.getOptionValue("k"); } else { System.out.println("Object key unspecified"); continue; } readOnlyTransaction(fac, key); } else if ("put".equals(cmdArgs[0])) { if (cmd.hasOption("k")) { key = cmd.getOptionValue("k"); } else { System.out.println("Object key unspecified"); continue; } if (cmd.hasOption("v")) { value = cmd.getOptionValue("v"); } else { System.out.println("Object value unspecified"); continue; } blindWriteTransaction(fac, key, value); } else if ("load".equals(cmdArgs[0])) { String numberKeys, concurrent; if (cmd.hasOption("n")) { numberKeys = cmd.getOptionValue("n"); } else { System.out.println("Number of keys unspecified"); continue; } if (cmd.hasOption("c")) { concurrent = cmd.getOptionValue("c"); } else { System.out.println("No of threads unspecified"); continue; } if (cmd.hasOption("silent")) { silent.compareAndSet(false, true); } // Get the number of keys int numKeys = Integer.parseInt(numberKeys); int concurrency1 = Integer.parseInt(concurrent); loadData(fac, numKeys, concurrency1); } else if ("bigLoad".equals(cmdArgs[0])) { String numberKeys; if (cmd.hasOption("n")) { numberKeys = cmd.getOptionValue("n"); } else { System.out.println("Number of keys unspecified"); continue; } if (cmd.hasOption("silent")) { silent.compareAndSet(false, true); } // Get the number of keys int numKeys = Integer.parseInt(numberKeys); bigLoad(fac, numKeys); } else if ("getr".equals(cmdArgs[0])) { if (cmd.hasOption("c")) { concurrency = Integer.parseInt(cmd.getOptionValue("c")); } if (cmd.hasOption("n")) { num = Integer.parseInt(cmd.getOptionValue("n")); } if (cmd.hasOption("t")) { total = Integer.parseInt(cmd.getOptionValue("t")); } if (cmd.hasOption("w")) { keysPerWorker = Integer.parseInt(cmd.getOptionValue("w")); } if (cmd.hasOption("silent")) { silent.compareAndSet(false, true); } randomReadOnlyTransactions(fac, concurrency, num, keysPerWorker, total); silent.compareAndSet(true, false); } else if ("putr".equals(cmdArgs[0])) { if (cmd.hasOption("c")) { concurrency = Integer.parseInt(cmd.getOptionValue("c")); } if (cmd.hasOption("n")) { num = Integer.parseInt(cmd.getOptionValue("n")); } if (cmd.hasOption("t")) { total = Integer.parseInt(cmd.getOptionValue("t")); } if (cmd.hasOption("w")) { keysPerWorker = Integer.parseInt(cmd.getOptionValue("w")); } if (cmd.hasOption("silent")) { silent.compareAndSet(false, true); } randomBlindWriteTransactions(fac, concurrency, num, keysPerWorker, total); silent.compareAndSet(true, false); } else if ("delete".equals(cmdArgs[0])) { if (cmd.hasOption("k")) { key = cmd.getOptionValue("k"); } else { System.out.println("Object key unspecified"); continue; } blindWriteTransaction(fac, key, Database.DELETE_VALUE_STRING); } else if ("primary".equals(cmdArgs[0])) { int shardId = 0; String serverId; if (cmd.hasOption("s")) { shardId = Integer.parseInt(cmd.getOptionValue("s")); if (shardId >= config.getShards()) { System.out.println("Invalid shard ID"); continue; } } if (cmd.hasOption("p")) { serverId = cmd.getOptionValue("p"); } else { System.out.println("Primary server ID not specified"); continue; } if (!config.reorderMembers(shardId, serverId)) { System.out.println("Invalid server ID: " + cmdArgs[1]); } if (fac.isLocal()) { Member primary = config.getMembers(shardId)[0]; System.out.print("Primary Storage Node: " + primary.getProcessId() + " ["); System.out.println(primary.getHostName() + ":" + primary.getPort() + "]"); } else { System.out.print("Primary App Server: " + config.getAppServerUrl()); } } else if ("servers".equals(cmdArgs[0])) { if (fac.isLocal()) { int shards = config.getShards(); for (int i = 0; i < shards; i++) { boolean first = true; System.out.println("Shard " + i); System.out.println("======="); for (Member member : config.getMembers(i)) { System.out.print(member.getProcessId() + " ["); System.out.print(member.getHostName() + ":" + member.getPort() + "]"); if (first) { System.out.print(" [Primary]"); first = false; } System.out.println(); } } } else { System.out.println("App Server: " + config.getAppServerUrl()); } } else if ("shard".equals(cmdArgs[0])) { if (cmd.hasOption("k")) { key = cmd.getOptionValue("k"); int shardId = config.getShardId(key); System.out.println("Shard ID: " + shardId); boolean first = true; for (Member member : config.getMembers(shardId)) { System.out.print(member.getProcessId() + " ["); System.out.print(member.getHostName() + ":" + member.getPort() + "]"); if (first) { System.out.print(" [Primary]"); first = false; } System.out.println(); } } else { System.out.println("Object key unspecified"); } } else if ("quit".equals(cmdArgs[0])) { break; } else if ("help".equals(cmdArgs[0])) { System.out.println("get -k key Retrieve the object with the specified key\n"); System.out.println( "put -k key -v value Write the given value to the object with the specified key\n"); System.out.println("getr -c concur -n requests -t totalKeys -w keysPerWorker"); System.out.println( " Execute random read operations using multiple concurrent workers\n"); System.out.println("putr -c concur -n requests -t totalKeys -w keysPerWorker"); System.out.println( " Execute random write operations using multiple concurrent workers\n"); System.out.println("delete -k key Delete the specified object\n"); System.out.println("primary [serverId] Display/Set the primary backend server\n"); System.out.println("servers List all the backend servers\n"); System.out.println( "shard -k key Describe the shard a particular key belongs to\n"); System.out.println("help Display this help message\n"); System.out.println("quit Terminate the client application\n"); } else { System.out.println("Unrecognized command: " + command); } } exec.shutdownNow(); fac.close(); }