Java Code Examples for com.google.cloud.storage.StorageException#getMessage()

The following examples show how to use com.google.cloud.storage.StorageException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GCSErrorWriter.java    From beast with Apache License 2.0 6 votes vote down vote up
@Override
public Status writeErrorRecords(List<Record> errorRecords) {
    if (errorRecords.isEmpty()) {
        return new WriteStatus(true, Optional.ofNullable(null));
    }
    final Instant startTime = Instant.now();
    try {
        //Store messages in the GCS
        storeMessagesInGCS(errorRecords);
    } catch (StorageException se) {
        log.error("Exception::Failed to write to GCS: {} records size: {}", se, errorRecords.size());
        throw new BQErrorHandlerException(se.getMessage());
    }
    statsClient.timeIt("sink.gcs.push.invalid.time", startTime);
    //alter the insert status - as successful
    return new WriteStatus(true, Optional.ofNullable(null));
}
 
Example 2
Source File: GCSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public Note get(String noteId, String notePath, AuthenticationInfo subject) throws IOException {
  BlobId blobId = makeBlobId(noteId, notePath);
  byte[] contents;
  try {
    contents = storage.readAllBytes(blobId);
  } catch (StorageException se) {
    throw new IOException("Could not read " + blobId.toString() + ": " + se.getMessage(), se);
  }

  try {
    return Note.fromJson(new String(contents, encoding));
  } catch (JsonParseException jpe) {
    throw new IOException(
        "Could note parse as json " + blobId.toString() + jpe.getMessage(), jpe);
  }
}
 
Example 3
Source File: OldGCSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public Note get(String noteId, AuthenticationInfo subject) throws IOException {
  BlobId blobId = makeBlobId(noteId);
  byte[] contents;
  try {
    contents = storage.readAllBytes(blobId);
  } catch (StorageException se) {
    throw new IOException("Could not read " + blobId.toString() + ": " + se.getMessage(), se);
  }

  try {
    return Note.fromJson(new String(contents, encoding));
  } catch (JsonParseException jpe) {
    throw new IOException(
        "Could note parse as json " + blobId.toString() + jpe.getMessage(), jpe);
  }
}
 
Example 4
Source File: GCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, NoteInfo> list(AuthenticationInfo subject) throws IOException {
  try {
    Map<String, NoteInfo> infos = new HashMap<>();
    Iterable<Blob> blobsUnderDir;
    if (basePath.isPresent()) {
      blobsUnderDir = storage
        .list(bucketName, BlobListOption.prefix(this.basePath.get() + "/"))
        .iterateAll();
    } else {
      blobsUnderDir = storage
        .list(bucketName)
        .iterateAll();
    }
    for (Blob b : blobsUnderDir) {
      Matcher matcher = notePathPattern.matcher(b.getName());
      if (matcher.matches()) {
        // Callers only use the id field, so do not fetch each note
        // This matches the implementation in FileSystemNoteRepo#list
        String noteFileName = matcher.group(1);
        try {
          String noteId = getNoteId(noteFileName);
          String notePath = getNotePath("", noteFileName);
          infos.put(noteId, new NoteInfo(noteId, notePath));
        } catch (IOException e) {
          LOGGER.warn(e.getMessage());
        }
      }
    }
    return infos;
  } catch (StorageException se) {
    throw new IOException("Could not list GCS directory: " + se.getMessage(), se);
  }
}
 
Example 5
Source File: GCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
  BlobInfo info = BlobInfo.newBuilder(makeBlobId(note.getId(), note.getPath()))
      .setContentType("application/json")
      .build();
  try {
    storage.create(info, note.toJson().getBytes("UTF-8"));
  } catch (StorageException se) {
    throw new IOException("Could not write " + info.toString() + ": " + se.getMessage(), se);
  }
}
 
Example 6
Source File: GCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String noteId, String notePath, AuthenticationInfo subject) throws IOException {
  Preconditions.checkArgument(!Strings.isNullOrEmpty(noteId));
  BlobId blobId = makeBlobId(noteId, notePath);
  try {
    boolean deleted = storage.delete(blobId);
    if (!deleted) {
      throw new IOException("Tried to remove nonexistent blob " + blobId.toString());
    }
  } catch (StorageException se) {
    throw new IOException("Could not remove " + blobId.toString() + ": " + se.getMessage(), se);
  }
}
 
Example 7
Source File: OldGCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public List<OldNoteInfo> list(AuthenticationInfo subject) throws IOException {
  try {
    List<OldNoteInfo> infos = new ArrayList<>();
    Iterable<Blob> blobsUnderDir;
    if (basePath.isPresent()) {
      blobsUnderDir = storage
        .list(bucketName, BlobListOption.prefix(this.basePath.get() + "/"))
        .iterateAll();
    } else {
      blobsUnderDir = storage
        .list(bucketName)
        .iterateAll();
    }
    for (Blob b : blobsUnderDir) {
      Matcher matcher = noteNamePattern.matcher(b.getName());
      if (matcher.matches()) {
        // Callers only use the id field, so do not fetch each note
        // This matches the implementation in FileSystemNoteRepo#list
        infos.add(new OldNoteInfo(matcher.group(1), "", null));
      }
    }
    return infos;
  } catch (StorageException se) {
    throw new IOException("Could not list GCS directory: " + se.getMessage(), se);
  }
}
 
Example 8
Source File: OldGCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
  BlobInfo info = BlobInfo.newBuilder(makeBlobId(note.getId()))
      .setContentType("application/json")
      .build();
  try {
    storage.create(info, note.toJson().getBytes("UTF-8"));
  } catch (StorageException se) {
    throw new IOException("Could not write " + info.toString() + ": " + se.getMessage(), se);
  }
}
 
Example 9
Source File: OldGCSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String noteId, AuthenticationInfo subject) throws IOException {
  Preconditions.checkArgument(!Strings.isNullOrEmpty(noteId));
  BlobId blobId = makeBlobId(noteId);
  try {
    boolean deleted = storage.delete(blobId);
    if (!deleted) {
      throw new IOException("Tried to remove nonexistent blob " + blobId.toString());
    }
  } catch (StorageException se) {
    throw new IOException("Could not remove " + blobId.toString() + ": " + se.getMessage(), se);
  }
}
 
Example 10
Source File: SnowflakeGCSClient.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
@Override
public void handleStorageException(Exception ex,
                                   int retryCount,
                                   String operation,
                                   SFSession connection,
                                   String command)
throws SnowflakeSQLException
{
  // no need to retry if it is invalid key exception
  if (ex.getCause() instanceof InvalidKeyException)
  {
    // Most likely cause is that the unlimited strength policy files are not installed
    // Log the error and throw a message that explains the cause
    SnowflakeFileTransferAgent.throwJCEMissingError(operation, ex);
  }


  if (ex instanceof StorageException)
  {
    StorageException se = (StorageException) ex;

    if (se.getCode() == 403 && connection != null && command != null)
    {
      // A 403 indicates that the access token has expired,
      // we need to refresh the GCS client with the new token
      SnowflakeFileTransferAgent.renewExpiredToken(connection, command, this);
    }

    // If we have exceeded the max number of retries, propagate the error
    if (retryCount > getMaxRetries())
    {
      throw new SnowflakeSQLException(se, SqlState.SYSTEM_ERROR,
                                      ErrorCode.GCP_SERVICE_ERROR.getMessageCode(),
                                      operation,
                                      se.getCode(),
                                      se.getMessage(),
                                      se.getReason());
    }
    else
    {
      logger.debug("Encountered exception ({}) during {}, retry count: {}",
                   ex.getMessage(), operation, retryCount);
      logger.debug("Stack trace: ", ex);

      // exponential backoff up to a limit
      int backoffInMillis = getRetryBackoffMin();

      if (retryCount > 1)
      {
        backoffInMillis <<= (Math.min(retryCount - 1,
                                      getRetryBackoffMaxExponent()));
      }

      try
      {
        logger.debug("Sleep for {} milliseconds before retry", backoffInMillis);

        Thread.sleep(backoffInMillis);
      }
      catch (InterruptedException ex1)
      {
        // ignore
      }

      if (se.getCode() == 403 && connection != null && command != null)
      {
        // A 403 indicates that the access token has expired,
        // we need to refresh the GCS client with the new token
        SnowflakeFileTransferAgent.renewExpiredToken(connection, command, this);
      }
    }
  }
  else
  {
    if (ex instanceof InterruptedException ||
        SnowflakeUtil.getRootCause(ex) instanceof SocketTimeoutException)
    {
      if (retryCount > getMaxRetries())
      {
        throw new SnowflakeSQLException(ex, SqlState.SYSTEM_ERROR,
                                        ErrorCode.IO_ERROR.getMessageCode(),
                                        "Encountered exception during " + operation + ": " +
                                        ex.getMessage());
      }
      else
      {
        logger.debug("Encountered exception ({}) during {}, retry count: {}",
                     ex.getMessage(), operation, retryCount);
      }
    }
    else
    {
      throw new SnowflakeSQLException(ex, SqlState.SYSTEM_ERROR,
                                      ErrorCode.IO_ERROR.getMessageCode(),
                                      "Encountered exception during " + operation + ": " +
                                      ex.getMessage());
    }
  }
}