public Command newCommand( CommandType type, Set<Byte> involvedPartitions, Map<Byte, Long> instanceMap) { Command c = new Command( type.getValue(), new Random().nextLong(), (int) (System.currentTimeMillis() / 1000L), involvedPartitions); c.setInstanceMap(instanceMap); return c; }
@Override public Response truncate(String path, long size, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Command cmd = newCommand(CommandType.TRUNCATE, parts, instanceMap); TruncateCmd truncate = new TruncateCmd(path, size, parts); cmd.setTruncate(truncate); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response chmod(String path, int mode, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Command cmd = newCommand(CommandType.CHMOD, parts, instanceMap); ChmodCmd chmod = new ChmodCmd(path, mode, parts); cmd.setChmod(chmod); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response release(String path, FileHandle fh, int flags, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Command cmd = newCommand(CommandType.RELEASE, parts, instanceMap); ReleaseCmd release = new ReleaseCmd(path, fh, flags, parts); cmd.setRelease(release); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response open(String path, int flags, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Command cmd = newCommand(CommandType.OPEN, parts, instanceMap); OpenCmd open = new OpenCmd(path, flags, parts); cmd.setOpen(open); FileHandle fh = (FileHandle) replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); r.setOpen(fh); return r; }
@Override public Response writeBlocks( String path, FileHandle fh, long offset, List<DBlock> blocks, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Command cmd = newCommand(CommandType.WRITE_BLOCKS, parts, instanceMap); WriteBlocksCmd write = new WriteBlocksCmd(path, fh, offset, blocks, parts); cmd.setWrite(write); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response rmdir(String path, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Set<Byte> parentParts = oracle.partitionsOf(Paths.dirname(path)); Set<Byte> involvedPartitions = Sets.union(parts, parentParts); Command cmd = newCommand(CommandType.RMDIR, involvedPartitions, instanceMap); RmdirCmd rmdir = new RmdirCmd(path, parentParts, parts); cmd.setRmdir(rmdir); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response getattr(String path, Map<Byte, Long> instanceMap) throws TException { // can be sent to ANY partition that replicates the path - we send it to the first returned by // the oracle path = makePathAbsolute(path); Set<Byte> parts = Sets.newHashSet(Byte.valueOf(partition)); Command cmd = newCommand(CommandType.ATTR, parts, instanceMap); AttrCmd attr = new AttrCmd(path, parts); cmd.setAttr(attr); Attr result = (Attr) replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); r.setGetattr(result); return r; }
@Override public Response readlink(String path, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); // can be sent to ANY partition that replicates the path - we send it to the first returned by // the oracle Set<Byte> parts = Sets.newHashSet(Byte.valueOf(partition)); Command cmd = newCommand(CommandType.READLINK, parts, instanceMap); ReadlinkCmd readlink = new ReadlinkCmd(path, parts); cmd.setReadlink(readlink); String result = (String) replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); r.setReadlink(result); return r; }
@Override public Response mknod( String path, int mode, int rdev, int uid, int gid, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); Set<Byte> parts = oracle.partitionsOf(path); Set<Byte> parentParts = oracle.partitionsOf(Paths.dirname(path)); Set<Byte> involvedPartitions = Sets.union(parts, parentParts); Command cmd = newCommand(CommandType.MKNOD, involvedPartitions, instanceMap); MknodCmd mknod = new MknodCmd(path, mode, uid, gid, parentParts, parts); cmd.setMknod(mknod); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response getdir(String path, Map<Byte, Long> instanceMap) throws TException { // can be sent to ANY partition that replicates the path - we send it to the first returned by // the oracle path = makePathAbsolute(path); Set<Byte> parts = Sets.newHashSet(Byte.valueOf(partition)); Command cmd = newCommand(CommandType.GETDIR, parts, instanceMap); GetdirCmd getdir = new GetdirCmd(path, parts); cmd.setGetdir(getdir); @SuppressWarnings("unchecked") List<DirEntry> entries = (List<DirEntry>) replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); r.setGetdir(entries); return r; }
@Override public Response readBlocks( String path, FileHandle fh, long offset, long bytes, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); // assuming this replica replicates the file, send to own partition Set<Byte> parts = Sets.newHashSet(Byte.valueOf(partition)); Command cmd = newCommand(CommandType.READ_BLOCKS, parts, instanceMap); ReadBlocksCmd read = new ReadBlocksCmd(path, fh, offset, bytes, parts); cmd.setRead(read); ReadResult readResult = (ReadResult) replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); r.setReadBlocks(readResult); return r; }
@Override public Response symlink(String target, String path, Map<Byte, Long> instanceMap) throws TException { path = makePathAbsolute(path); target = makePathAbsolute(target); Set<Byte> parts = oracle.partitionsOf(path); Set<Byte> parentParts = oracle.partitionsOf(Paths.dirname(path)); Set<Byte> involvedPartitions = Sets.union(parts, parentParts); Command cmd = newCommand(CommandType.SYMLINK, involvedPartitions, instanceMap); SymlinkCmd symlink = new SymlinkCmd(target, path, parentParts, parts); cmd.setSymlink(symlink); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response debug(Debug debug) throws TException { Set<Byte> partitions; if (debug.getType() == DebugCommands.POPULATE_FILE.getId()) { partitions = oracle.partitionsOf("/"); } else { partitions = new HashSet<>(); partitions.add(Byte.valueOf(Byte.parseByte(debug.getData().get("partition")))); } Command cmd = newCommand(CommandType.DEBUG, partitions, null); cmd.setDebug(debug); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }
@Override public Response rename(String from, String to, Map<Byte, Long> instanceMap) throws TException { from = makePathAbsolute(from); to = makePathAbsolute(to); Set<Byte> fromParts = oracle.partitionsOf(from); Set<Byte> fromParentParts = oracle.partitionsOf(Paths.dirname(from)); Set<Byte> toParts = oracle.partitionsOf(to); Set<Byte> toParentParts = oracle.partitionsOf(Paths.dirname(to)); Set<Byte> involvedPartitions = new HashSet<>(); involvedPartitions.addAll(fromParts); involvedPartitions.addAll(fromParentParts); involvedPartitions.addAll(toParts); involvedPartitions.addAll(toParentParts); Command cmd = newCommand(CommandType.RENAME, involvedPartitions, instanceMap); RenameCmd rename = new RenameCmd(from, to, fromParentParts, fromParts, toParentParts, toParts); cmd.setRename(rename); replica.submitCommand(cmd); Response r = new Response(replica.getInstanceMap()); return r; }