/** * Takes any Exception and rethrows it if it is an IOException, or wraps it in a FijiIOException * and throws that. * * @param e exception to be wrapped. * @throws IOException if the provided exception is an IOException. * @throws FijiIOException for any other Exception type. */ public static void wrapAndRethrow(Exception e) throws IOException { if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } else if (e instanceof IOException) { throw (IOException) e; } else if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new FijiIOException(e); } }
/** {@inheritDoc} */ @Override public FijiTableReader openTableReader() { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot open a table reader on a FijiTable in state %s.", state); try { return HBaseFijiTableReader.create(this); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public FijiTableWriter openTableWriter() { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot open a table writer on a FijiTable in state %s.", state); try { return new HBaseFijiTableWriter(this); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public CassandraFijiTableReader openTableReader() { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot open a table reader on a FijiTable in state %s.", state); try { return CassandraFijiTableReader.create(this); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public Entry<T> next() { final Entry<T> entry = mNext; if (entry == null) { throw new NoSuchElementException(); } try { mNext = getNext(); } catch (IOException ioe) { throw new FijiIOException(ioe); } return entry; }
/** {@inheritDoc} */ @Override public Iterator<FijiCell<T>> iterator() { try { return new FijiCellIterator<T>( mRowData, mColumnName, mHBaseColumnNameTranslator); } catch (IOException ex) { throw new FijiIOException(ex); } } }
/** {@inheritDoc} */ @Override public FijiTableWriter openTableWriter() { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot open a table writer on a FijiTable in state %s.", state); try { return new CassandraFijiTableWriter(this); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public FijiCell<T> next() { final FijiCell<T> next = mNextCell; if (null == next) { throw new NoSuchElementException(); } else { try { mNextCell = getNextCell(); } catch (IOException ioe) { throw new FijiIOException(ioe); } return next; } }
/** {@inheritDoc} */ @Override public Entry<Long, T> next() { final Entry<Long, T> entry = mNext; if (entry == null) { throw new NoSuchElementException(); } try { mNext = getNext(); } catch (IOException ioe) { throw new FijiIOException(ioe); } return entry; }
/** * Returns the Unix process ID of this JVM. * * @return the Unix process ID of this JVM. */ public static int getPid() { try { final Process process = new ProcessBuilder("/bin/sh", "-c", "echo $PPID").start(); try { Preconditions.checkState(process.waitFor() == 0); } catch (InterruptedException ie) { throw new RuntimeInterruptedException(ie); } final String pidStr = IOUtils.toString(process.getInputStream()).trim(); return Integer.parseInt(pidStr); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** * Generates a unique ID for this JVM. * * <p> The ID is formatted as "hostname;unix-pid;timestamp". </p> * * @return a unique ID for this JVM. */ private static String generateJvmId() { try { final String hostname = InetAddress.getLocalHost().getHostName(); final int pid = getPid(); final long timestamp = System.currentTimeMillis(); return String.format("%s;%d;%d", hostname, pid, timestamp); } catch (UnknownHostException uhe) { throw new FijiIOException(uhe); } }
/** * Start this table layout tracker. Asynchronously updates the handler with the initial layout. * * @return this. * @throws IOException on unrecoverable ZooKeeper error. */ public TableLayoutTracker start() throws IOException { try { mCache.start(true); mExecutor.execute(new Runnable() { @Override public void run() { mHandler.update(getLayoutID()); } }); return this; } catch (IOException ioe) { throw ioe; } catch (Exception e) { throw new FijiIOException(e); } }
/** * Deserialize a {@code FijiRowFilter} from JSON that has been constructed * using {@link #toJson}. * * @param root A {@code JsonNode} created by {@link #toJson} * @return A {@code FijiRowFilter} represented by the JSON * @throws FijiIOException in case the filter cannot be instantiated */ public static FijiRowFilter toFilter(JsonNode root) { final String filterDeserializerClassName = root.path(DESERIALIZER_CLASS_NODE).getTextValue(); try { final Class<?> filterDeserializerClass = Class.forName(filterDeserializerClassName); final FijiRowFilterDeserializer filterDeserializer = (FijiRowFilterDeserializer) filterDeserializerClass.newInstance(); final FijiRowFilter filter = filterDeserializer.createFromJson(root.path(FILTER_NODE)); return filter; } catch (ClassNotFoundException cnfe) { throw new FijiIOException(cnfe); } catch (IllegalAccessException iae) { throw new FijiIOException(iae); } catch (InstantiationException ie) { throw new FijiIOException(ie); } }
/** * Get the next HBase Result from the ResultScanner, reopening the scanner if necessary. * * @return the next HBase Result from the ResultScanner. */ private Result getNextResult() { for (int retries = 0; retries < MAX_RETRIES_ON_TIMEOUT; ++retries) { try { return mResultScanner.next(); } catch (LeaseException le) { reopenScanner(); } catch (ScannerTimeoutException ste) { reopenScanner(); } catch (IOException ioe) { throw new FijiIOException(ioe); } } throw new FijiIOException(String.format( "Unable to get Result from HBase scanner after %d attempts.", MAX_RETRIES_ON_TIMEOUT)); }
/** * Factory for ZooKeeper session clients. * * <p> This returns immediately, but the ZooKeeper session is established asynchronously. </p> * * @throws FijiIOException on I/O error. */ private void createZKClient() { LOG.debug("Creating new ZooKeeper client for address {} in {}.", mZKAddress, this); try { synchronized (mMonitor) { mZKClient = new ZooKeeper(mZKAddress, SESSION_TIMEOUT, new SessionWatcher()); } } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public FijiRowData next() { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot get next on FijiRowScanner instance in state %s.", state); if (mNextResult == null) { // Comply with the Iterator interface: throw new NoSuchElementException(); } final Result result = mNextResult; mLastReturnedKey = result.getRow(); // Prefetch the next row for hasNext(): mNextResult = getNextResult(); // Decode the HBase result into a FijiRowData: try { final EntityId entityId = mEntityIdFactory.getEntityIdFromHBaseRowKey(result.getRow()); return new HBaseFijiRowData(mTable, mDataRequest, entityId, result, mCellDecoderProvider); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
/** {@inheritDoc} */ @Override public HConnection getHConnection(Fiji fiji) { try { return HConnectionManager.createConnection(fiji.getConf()); } catch (IOException ioe) { throw new FijiIOException("Couldn't create an HConnection.", ioe); } }
/** * Deserialize a {@code FijiRowFilter} from JSON that has been constructed * using {@link #toJson}. * * @param json A JSON String created by {@link #toJson} * @return A {@code FijiRowFilter} represented by the JSON * @throws FijiIOException in case the json cannot be read or the filter * cannot be instantiated */ public static FijiRowFilter toFilter(String json) { final ObjectMapper mapper = new ObjectMapper(); try { final JsonNode root = mapper.readTree(json); return toFilter(root); } catch (IOException ioe) { throw new FijiIOException(ioe); } }
if (!mReopenScannerOnTimeout) { LOG.debug("HBase scanner timed out and user disabled automatic scanner reopening."); throw new FijiIOException( "HBase scanner timed out and user disabled automatic scanner reopening.", le); } else { if (!mReopenScannerOnTimeout) { LOG.debug("HBase scanner timed out and user disabled automatic scanner reopening."); throw new FijiIOException( "HBase scanner timed out and user disabled automatic scanner reopening.", ste); } else { throw new FijiIOException(ioe); throw new FijiIOException("Unable to retrieve HBase result from scanner.");
/** {@inheritDoc} */ @Override public FijiResult<T> next() { final State oldState = mState.get(); Preconditions.checkState(oldState == State.OPEN, "Cannot get element from FijiResultScanner in state %s.", oldState); final Result next = mNextResult; if (null == next) { throw new NoSuchElementException(); } mNextResult = getNextResult(); try { return HBaseFijiResult.create( mEidFactory.getEntityIdFromHBaseRowKey(next.getRow()), mRequest, next, mTable, mLayout, mColumnNameTranslator, mDecoderProvider); } catch (IOException e) { throw new FijiIOException(e); } }