Skip to content

optimize routing algorithm #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import dnl.utils.text.table.TextTable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
Expand All @@ -28,9 +27,9 @@ public class MountsManager extends AbstractService {
private static final Logger LOG = LoggerFactory.getLogger(MountsManager.class);

static class MountEntry {
final String fsUri;
final String mountPoint;
final String[] attributes;
public final String fsUri;
public final String mountPoint;
public final String[] attributes;

public MountEntry(String fsUri, String mountPoint, String[] attributes) {
this.fsUri = fsUri;
Expand All @@ -57,6 +56,8 @@ public String toString() {
ImmutableList<String> allFs;
MountEntry root;
NodeCache nodeCache;
Map<String, List<MountEntry>> lookupMap;
Random rand;

@VisibleForTesting protected volatile boolean installed;

Expand Down Expand Up @@ -90,6 +91,7 @@ protected void serviceInit(Configuration conf) throws Exception {
sessionTimeout,
connectionTimeout,
new ExponentialBackoffRetry(retryBaseSleep, maxRetries));
rand = new Random();
installed = false;
}

Expand Down Expand Up @@ -119,6 +121,43 @@ public String resolve(String path) {
return chosen.fsUri;
}

public String resolveOpt(String path) {
MountEntry chosen = null;
if (path == null) {
chosen = root;
} else {
chosen = resolveParentPath(path, path);
if (chosen == null) {
StringBuilder seg = new StringBuilder(path.length());
seg.append(path);
for (int i = path.length() - 1; i >= 0; i--) {
if (path.charAt(i) == '/') {
seg.setLength(i);
MountEntry entry = resolveParentPath(seg.toString(), path);
if (entry != null) {
chosen = entry;
break;
}
}
}
}
}
if (chosen == null) {
chosen = root;
}
return chosen.fsUri;
}

private MountEntry resolveParentPath(String parent, String path) {
Map<String, List<MountEntry>> entries = this.lookupMap;
List<MountEntry> mounts = entries.get(parent);
if (mounts == null) {
LOG.debug("resolve not found");
return null;
}
return mounts.get(rand.nextInt(mounts.size()));
}

/**
* Determine whether given path is exactly a valid mount point
*
Expand Down Expand Up @@ -166,6 +205,7 @@ protected void installMountTable(List<MountEntry> entries) {
}
this.allFs = ImmutableList.copyOf(fs);
this.mounts = ImmutableList.copyOf(entries);
this.lookupMap = buildLookupMap(entries);
this.installed = true;
}

Expand Down Expand Up @@ -256,4 +296,20 @@ public void dump() {
public void load(String mounts) throws Exception {
framework.setData().forPath(zkMountTablePath, mounts.getBytes());
}

protected Map<String, List<MountEntry>> buildLookupMap(List<MountEntry> entries) {
Map<String, List<MountEntry>> lookupMap = new HashMap<>();
for (MountEntry entry : entries) {
List<MountEntry> mounts = lookupMap.get(entry.mountPoint);
if (mounts == null) {
mounts = new ArrayList<>();
lookupMap.put(entry.mountPoint, mounts);
}
mounts.add(entry);
if (entry.mountPoint.equals("/")) {
lookupMap.put("", mounts);
}
}
return lookupMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RouteInfo route(String path) throws IOException {
logicalPath = "/" + mch.group(1);
LOG.debug("Hit trash pattern: " + path + " -> " + logicalPath);
}
String fs = nnProxy.getMounts().resolve(logicalPath);
String fs = nnProxy.getMounts().resolveOpt(logicalPath);
if (fs == null) {
// mount to default path
fs = defaultNN;
Expand Down