/** * @param errs Error. * @return {@code True} if error was caused by some connection IO error. */ private static boolean connectionError(IgniteCheckedException errs) { return X.hasCause(errs, ConnectException.class, HandshakeException.class, SocketTimeoutException.class, HandshakeTimeoutException.class, IgniteSpiOperationTimeoutException.class); }
/** * @param e Exception. */ private void checkSqlException(IgniteCheckedException e) { IgniteSQLException sqlEx = X.cause(e, IgniteSQLException.class); if(sqlEx != null) throw sqlEx; }
/** * Gets the entire nested stack-trace of an throwable. * * @param throwable The {@code Throwable} to be examined. * @return The nested stack trace, with the root cause first. */ public static String getFullStackTrace(Throwable throwable) { return X.getFullStackTrace(throwable); }
/** * Writes the question end read the boolean answer from the console. * * @param question Question to write. * @return {@code true} if user inputs 'Y' or 'y', {@code false} otherwise. */ private static boolean ask(String question) { X.println(); X.print(" < " + question + " (Y/N): "); String answer = null; if (!F.isEmpty(System.getenv("IGNITE_HADOOP_SETUP_YES"))) answer = "Y"; else { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { answer = br.readLine(); } catch (IOException e) { exit("Failed to read answer: " + e.getMessage(), e); } } if (answer != null && "Y".equals(answer.toUpperCase().trim())) { X.println(" > Yes."); return true; } else { X.println(" > No."); return false; } }
/** * Depending on whether or not log is provided and quiet mode is enabled logs given * messages as quiet message or normal log WARN message. If {@code log} is {@code null} * or in QUIET mode it will add {@code (wrn)} prefix to the message. * * @param log Optional logger to use when QUIET mode is not enabled. * @param msg Message to log using normal logger. * @param e Optional exception. */ public static void warn(@Nullable IgniteLogger log, Object msg, @Nullable Throwable e) { assert msg != null; if (log != null) log.warning(compact(msg.toString()), e); else { X.println("[" + SHORT_DATE_FMT.format(new java.util.Date()) + "] (wrn) " + compact(msg.toString())); if (e != null) e.printStackTrace(System.err); else X.printerrln(); } }
/** * Prints message. * * @param msg Message. */ private static void println(String msg) { X.println(" > " + msg); }
/** * TODO proper exception handling after https://issues.apache.org/jira/browse/IGNITE-9470 * Checks if given exception was caused by MVCC write conflict. * * @param e Exception. */ public static void assertMvccWriteConflict(Exception e) { IgniteSQLException sqlEx = X.cause(e, IgniteSQLException.class); if (sqlEx == null || sqlEx.statusCode() != TRANSACTION_SERIALIZATION_ERROR) fail("Unexpected exception: " + X.getFullStackTrace(e)); }
/** * */ @Test public void testHasCause() { ConnectException conEx = new ConnectException(); IOException ioEx = new IOException(conEx); IgniteCheckedException gridEx = new IgniteCheckedException(ioEx); assert X.hasCause(gridEx, IOException.class, NumberFormatException.class); assert !X.hasCause(gridEx, NumberFormatException.class); assert X.cause(gridEx, IOException.class) == ioEx; assert X.cause(gridEx, ConnectException.class) == conEx; assert X.cause(gridEx, NumberFormatException.class) == null; assert gridEx.getCause(IOException.class) == ioEx; assert gridEx.getCause(ConnectException.class) == conEx; assert gridEx.getCause(NumberFormatException.class) == null; }
/** * @throws Exception If failed. */ @Test public void testFailedInit() throws Exception { for (int i = 0; i < ClientTestRestServer.SERVERS_CNT; i++) runServer(i, true); GridClient c = client(); try { c.compute().execute("fake", "arg"); fail("Client operation should fail when server resets connections."); } catch (GridClientDisconnectedException e) { assertTrue("Thrown exception doesn't have an expected cause: " + X.getFullStackTrace(e), X.hasCause(e, GridClientConnectionResetException.class, ClosedChannelException.class)); } for (int i = 0; i < ClientTestRestServer.SERVERS_CNT; i++) // Connection manager does 3 attempts to get topology before failure. assertEquals("Server: " + i, 3, srvs[i].getConnectCount()); }
/** * Alias for {@code System.err.println}. * * @param s1 First string to print. * @param rest Optional list of objects to print as well. */ public static void printerrln(@Nullable String s1, @Nullable Object... rest) { error(s1, rest); }
/** * Exits with code {@code -1} if maximum memory is below 90% of minimally allowed threshold. * * @param min Minimum memory threshold. */ public static void checkMinMemory(long min) { long maxMem = Runtime.getRuntime().maxMemory(); if (maxMem < .85 * min) { printerrln("Heap limit is too low (" + (maxMem / (1024 * 1024)) + "MB), please increase heap size at least up to " + (min / (1024 * 1024)) + "MB."); System.exit(-1); } }
/** * Depending on whether or not log is provided and quiet mode is enabled logs given * messages as quiet message or normal log ERROR message. If {@code log} is {@code null} * or in QUIET mode it will add {@code (err)} prefix to the message. * * @param log Optional logger to use when QUIET mode is not enabled. * @param longMsg Message to log using normal logger. * @param shortMsg Message to log using quiet logger. * @param e Optional exception. */ public static void error(@Nullable IgniteLogger log, Object longMsg, Object shortMsg, @Nullable Throwable e) { assert longMsg != null; assert shortMsg != null; if (log != null) { if (e == null) log.error(compact(longMsg.toString())); else log.error(compact(longMsg.toString()), e); } else { X.printerr("[" + SHORT_DATE_FMT.format(new java.util.Date()) + "] (err) " + compact(shortMsg.toString())); if (e != null) e.printStackTrace(System.err); else X.printerrln(); } }
@Override public void onConnected(GridNioSession ses) { X.print("New connection accepted."); }
/** * Writes error. * * @param ex Error. * @param writer Writer. */ public static void writeError(Throwable ex, BinaryRawWriterEx writer) { writer.writeObjectDetached(ex.getClass().getName()); writer.writeObjectDetached(ex.getMessage()); writer.writeObjectDetached(X.getFullStackTrace(ex)); PlatformNativeException nativeCause = X.cause(ex, PlatformNativeException.class); if (nativeCause != null) { writer.writeBoolean(true); writer.writeObjectDetached(nativeCause.cause()); } else writer.writeBoolean(false); }