Search in sources :

Example 1 with IgfsOutOfSpaceException

use of org.apache.ignite.igfs.IgfsOutOfSpaceException in project ignite by apache.

the class IgfsIpcHandler method processStreamControlRequest.

/**
 * Processes stream control request.
 *
 * @param ses Session.
 * @param cmd Command.
 * @param msg Message.
 * @param in Data input to read.
 * @return Response message if needed.
 * @throws IgniteCheckedException If failed.
 * @throws IOException If failed.
 */
private IgfsMessage processStreamControlRequest(IgfsClientSession ses, IgfsIpcCommand cmd, IgfsMessage msg, DataInput in) throws IgniteCheckedException, IOException {
    IgfsStreamControlRequest req = (IgfsStreamControlRequest) msg;
    Long rsrcId = req.streamId();
    IgfsControlResponse resp = new IgfsControlResponse();
    switch(cmd) {
        case CLOSE:
            {
                Closeable res = resource(ses, rsrcId);
                if (log.isDebugEnabled())
                    log.debug("Requested to close resource [igfsName=" + igfs.name() + ", rsrcId=" + rsrcId + ", res=" + res + ']');
                if (res == null)
                    throw new IgniteCheckedException("Resource to close not found: " + rsrcId);
                try {
                    res.close();
                } catch (IOException e) {
                    // Unwrap OutOfSpaceException, if has one.
                    IgfsOutOfSpaceException space = X.cause(e, IgfsOutOfSpaceException.class);
                    if (space != null)
                        throw space;
                    throw e;
                }
                boolean success = ses.unregisterResource(rsrcId, res);
                assert success : "Failed to unregister resource [igfsName=" + igfs.name() + ", rsrcId=" + rsrcId + ", res=" + res + ']';
                if (log.isDebugEnabled())
                    log.debug("Closed IGFS stream [igfsName=" + igfs.name() + ", streamId=" + rsrcId + ", ses=" + ses + ']');
                resp.response(true);
                break;
            }
        case READ_BLOCK:
            {
                long pos = req.position();
                int size = req.length();
                IgfsInputStreamImpl igfsIn = (IgfsInputStreamImpl) resource(ses, rsrcId);
                if (igfsIn == null)
                    throw new IgniteCheckedException("Input stream not found (already closed?): " + rsrcId);
                byte[][] chunks = igfsIn.readChunks(pos, size);
                resp.response(chunks);
                // Calculate number of read bytes.
                // len = len(first) + (n - 2) * len(block) + len(last).
                int len = 0;
                if (chunks.length > 0)
                    len += chunks[0].length;
                if (chunks.length > 1)
                    len += chunks[chunks.length - 1].length;
                if (chunks.length > 2)
                    len += chunks[1].length * (chunks.length - 2);
                resp.length(len);
                break;
            }
        case WRITE_BLOCK:
            {
                IgfsOutputStream out = (IgfsOutputStream) resource(ses, rsrcId);
                if (out == null)
                    throw new IgniteCheckedException("Output stream not found (already closed?): " + rsrcId);
                int writeLen = req.length();
                try {
                    out.transferFrom(in, writeLen);
                    if (errWrite)
                        throw new IOException("Failed to write data to server (test).");
                    // No response needed.
                    return null;
                } catch (IOException e) {
                    resp.error(rsrcId, e.getMessage());
                    break;
                }
            }
        default:
            assert false;
            break;
    }
    return resp;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Closeable(java.io.Closeable) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgfsStreamControlRequest(org.apache.ignite.internal.igfs.common.IgfsStreamControlRequest) IOException(java.io.IOException) IgfsOutOfSpaceException(org.apache.ignite.igfs.IgfsOutOfSpaceException) IgfsControlResponse(org.apache.ignite.internal.igfs.common.IgfsControlResponse) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream)

Aggregations

Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgfsOutOfSpaceException (org.apache.ignite.igfs.IgfsOutOfSpaceException)1 IgfsOutputStream (org.apache.ignite.igfs.IgfsOutputStream)1 IgfsControlResponse (org.apache.ignite.internal.igfs.common.IgfsControlResponse)1 IgfsStreamControlRequest (org.apache.ignite.internal.igfs.common.IgfsStreamControlRequest)1