use of org.neo4j.driver.v1.Session in project rdf2neo by Rothamsted.
the class Neo4jDataManager method runCypher.
/**
* <p>Runs a Cypher gommands against the current {@link #getNeo4jDriver()}.</p>
*
* <p>The keyVals parameter is passed to {@link Values#parameters(Object...)}.</p>
*
* <p>The command is wrapped into a single transaction, which is committed within the method.</p>
*
* <p>Because parallelism sometimes raises exceptions about race conditions, we use {@link MultipleAttemptsExecutor}
* to re-attempt the command execution a couple of times, after such exceptions.</p>
*/
public void runCypher(String cypher, Object... keyVals) {
if (log.isTraceEnabled())
log.trace("Cypher: {} params: {}", cypher, ArrayUtils.toString(keyVals));
// Re-attempt a couple of times, in case of exceptions due to deadlocks over locking nodes.
MultipleAttemptsExecutor attempter = new MultipleAttemptsExecutor(TransientException.class, DatabaseException.class, ServiceUnavailableException.class);
attempter.setMaxAttempts(10);
attempter.setMinPauseTime(30 * 1000);
attempter.setMaxPauseTime(3 * 60 * 1000);
attempter.execute(() -> {
try (Session session = this.neo4jDriver.session()) {
session.run(cypher, parameters(keyVals));
}
});
}
use of org.neo4j.driver.v1.Session in project rdf2neo by Rothamsted.
the class CypherHandlersIT method testNodes.
/**
* Test {@link CyNodeLoadingHandler} to see if nodes are mapped from RDF and loaded into Neo4J
*/
@Test
public void testNodes() throws Exception {
try (Driver neoDriver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "test"))) {
Session session = neoDriver.session(AccessMode.READ);
StatementResult cursor = session.run("MATCH ( n:TestNode ) RETURN COUNT ( n ) AS ct");
Assert.assertEquals("Wrong count for TestNode", 2, cursor.next().get("ct").asLong());
cursor = session.run("MATCH ( n:TestNode { iri:'" + iri("ex:2") + "'} ) RETURN properties ( n ) AS props");
assertTrue("ex:2 not returned!", cursor.hasNext());
Map<String, Object> map = cursor.next().get("props").asMap();
assertEquals("Wrong property!", "another string", map.get("attrib3"));
}
}
use of org.neo4j.driver.v1.Session in project cloudstack by apache.
the class SshHelper method sshExecute.
public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command, int connectTimeoutInMs, int kexTimeoutInMs, int waitResultTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.Session sess = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
sess = openConnectionSession(conn);
sess.execCommand(command);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) {
throwSshExceptionIfStdoutOrStdeerIsNull(stdout, stderr);
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs);
throwSshExceptionIfConditionsTimeout(conditions);
if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
break;
}
if (canEndTheSshConnection(waitResultTimeoutInMs, sess, conditions)) {
break;
}
}
while ((currentReadBytes = stdout.read(buffer)) != -1) {
sbResult.append(new String(buffer, 0, currentReadBytes));
}
while ((currentReadBytes = stderr.read(buffer)) != -1) {
sbResult.append(new String(buffer, 0, currentReadBytes));
}
}
String result = sbResult.toString();
if (StringUtils.isBlank(result)) {
try {
result = IOUtils.toString(stdout, StandardCharsets.UTF_8);
} catch (IOException e) {
s_logger.error("Couldn't get content of input stream due to: " + e.getMessage());
return new Pair<Boolean, String>(false, result);
}
}
if (sess.getExitStatus() == null) {
// Exit status is NOT available. Returning failure result.
s_logger.error(String.format("SSH execution of command %s has no exit status set. Result output: %s", command, result));
return new Pair<Boolean, String>(false, result);
}
if (sess.getExitStatus() != null && sess.getExitStatus().intValue() != 0) {
s_logger.error(String.format("SSH execution of command %s has an error status code in return. Result output: %s", command, result));
return new Pair<Boolean, String>(false, result);
}
return new Pair<Boolean, String>(true, result);
} finally {
if (sess != null)
sess.close();
if (conn != null)
conn.close();
}
}
use of org.neo4j.driver.v1.Session in project cloudstack by apache.
the class StressTestDirectAttach method sshTest.
private static String sshTest(String host, String password) {
int i = 0;
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
return null;
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
return null;
}
// We will retry 5 times before quitting
String result = null;
int retry = 0;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt. Account is " + s_account.get());
Thread.sleep(120000);
}
s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + s_account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + " + s_account.get() + " ssHed successfully into linux host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
return "Authentication failed";
}
boolean success = false;
String linuxCommand = null;
if (i % 10 == 0)
linuxCommand = "rm -rf *; wget http://192.168.1.250/dump.bin && ls -al dump.bin";
else
linuxCommand = "wget http://192.168.1.250/dump.bin && ls -al dump.bin";
Session sess = conn.openSession();
s_logger.info("User " + s_account.get() + " executing : " + linuxCommand);
sess.execCommand(linuxCommand);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (!success) {
retry++;
if (retry == MAX_RETRY_LINUX) {
result = "SSH Linux Network test fail";
}
}
return result;
} catch (Exception e) {
retry++;
s_logger.error("SSH Linux Network test fail with error");
if (retry == MAX_RETRY_LINUX) {
return "SSH Linux Network test fail with error " + e.getMessage();
}
}
i++;
}
}
use of org.neo4j.driver.v1.Session in project cloudstack by apache.
the class SshTest method main.
public static void main(String[] args) {
// Parameters
List<String> argsList = Arrays.asList(args);
Iterator<String> iter = argsList.iterator();
while (iter.hasNext()) {
String arg = iter.next();
if (arg.equals("-h")) {
host = iter.next();
}
if (arg.equals("-p")) {
password = iter.next();
}
if (arg.equals("-u")) {
url = iter.next();
}
}
if (host == null || host.equals("")) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
System.exit(2);
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
System.exit(2);
}
try {
s_logger.info("Attempting to SSH into host " + host);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + ssHed successfully into host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
System.exit(2);
}
String linuxCommand = "wget " + url;
Session sess = conn.openSession();
sess.execCommand(linuxCommand);
sess.close();
conn.close();
} catch (Exception e) {
s_logger.error("SSH test fail with error", e);
System.exit(2);
}
}
Aggregations