Skip to content

Commit

Permalink
- issue96: add helper for tag-aware sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
agirbal committed Nov 4, 2013
1 parent e8d4e8f commit 55d8904
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 22 deletions.
19 changes: 19 additions & 0 deletions resource/xml/umongo.xml
Expand Up @@ -767,6 +767,25 @@
<MenuItem id="rsStatus" call="rsStatus" label="Get Status"/>
<MenuItem id="rsOplogInfo" call="rsOplogInfo" label="Get Oplog Info"/>
</Menu>
<Menu id="sharding">
<MenuItem id="manageTags" call="manageTags" showDialog="false">
<FormDialog buttons="OK" okLabel="Done">
<Zone>
<Scroller zone="CENTER" preferredSize="320;-1">
<ListArea id="tagList" items=""/>
</Scroller>
<Div axis="Y" zone="EAST">
<Button id="addTag" icon="add.png" iconGroup="icons" toolTipText="Add a new tag" call="addTag">
<FormDialog id="addTagDialog">
<TextField id="atTag" label="Tag"/>
</FormDialog>
</Button>
<Button id="removeTag" icon="dialog-close_16.png" iconGroup="icons" toolTipText="Remove the tag" call="removeTag"/>
</Div>
</Zone>
</FormDialog>
</MenuItem>
</Menu>
<Menu id="tools">
<MenuItem id="queryOplog" call="queryOplog">
<FormDialog>
Expand Down
2 changes: 1 addition & 1 deletion src/com/edgytech/umongo/CollectionPanel.java
Expand Up @@ -1109,7 +1109,7 @@ public String getNS() {

@Override
public String getShortName() {
return "ShardDistribution";
return "Sharding Distribution";
}
}.addJob();
}
Expand Down
5 changes: 1 addition & 4 deletions src/com/edgytech/umongo/DbPanel.java
Expand Up @@ -445,10 +445,7 @@ void refreshUserList() {
public void manageUsers(ButtonBase button) {
FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.manageUsers)).getDialog();
refreshUserList();

if (!dialog.show()) {
return;
}
dialog.show();
}

public void addUser(ButtonBase button) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/edgytech/umongo/MongoNode.java
Expand Up @@ -79,7 +79,7 @@ protected void populateChildren() {
try {
CommandResult res = node.getServerDB().command(new BasicDBObject("isMaster", 1), mongo.getOptions());
if (res.containsField("setName")) {
addChild(new ReplSetNode(mongo.getReplicaSetStatus().getName(), mongo, false));
addChild(new ReplSetNode(mongo.getReplicaSetStatus().getName(), mongo, null));
added = true;
}
} catch (Exception e) {
Expand All @@ -89,7 +89,7 @@ protected void populateChildren() {
if (!added)
addChild(node);
} else {
addChild(new ReplSetNode(mongo.getReplicaSetStatus().getName(), mongo, false));
addChild(new ReplSetNode(mongo.getReplicaSetStatus().getName(), mongo, null));
}

if (specifiedDb) {
Expand Down
24 changes: 13 additions & 11 deletions src/com/edgytech/umongo/ReplSetNode.java
Expand Up @@ -19,7 +19,6 @@
import com.mongodb.*;
import java.util.List;
import java.util.logging.Level;
import javax.swing.ImageIcon;

/**
*
Expand All @@ -29,12 +28,12 @@ public class ReplSetNode extends BaseTreeNode {

Mongo mongo;
String name;
boolean isShard;
String shardName;

public ReplSetNode(String name, Mongo mongo, boolean isShard) {
public ReplSetNode(String name, Mongo mongo, String shardName) {
this.mongo = mongo;
this.name = name != null ? name : "Replica Set";
this.isShard = isShard;
this.shardName = shardName;
try {
xmlLoad(Resource.getXmlDir(), Resource.File.replSetNode, null);
} catch (Exception ex) {
Expand All @@ -43,10 +42,10 @@ public ReplSetNode(String name, Mongo mongo, boolean isShard) {
markStructured();
}

public ReplSetNode(String name, List<ServerAddress> addrs, MongoOptions opts, boolean isShard) {
public ReplSetNode(String name, List<ServerAddress> addrs, MongoOptions opts, String shardName) {
this.mongo = new Mongo(addrs, opts);
this.name = name != null ? name : "Replica Set";
this.isShard = isShard;
this.shardName = shardName;
try {
xmlLoad(Resource.getXmlDir(), Resource.File.replSetNode, null);
} catch (Exception ex) {
Expand Down Expand Up @@ -92,13 +91,16 @@ public String getName() {
return name;
}

public String getShardName() {
return shardName;
}

@Override
protected void updateNode() {
if (isShard)
label = "Shard";
else
label = "ReplSet";
label += ": " + getName();
label = "";
if (shardName != null)
label += "Shard: " + shardName + " / ";
label += "ReplSet: " + getName();
}

@Override
Expand Down
119 changes: 118 additions & 1 deletion src/com/edgytech/umongo/ReplSetPanel.java
Expand Up @@ -53,7 +53,12 @@ enum Item {
queryOplog,
qoStart,
qoEnd,
qoQuery
qoQuery,
manageTags,
tagList,
addTag,
atTag,
removeTag
}

public ReplSetPanel() {
Expand Down Expand Up @@ -305,4 +310,116 @@ public void queryOplog(ButtonBase button) {

CollectionPanel.doFind(oplog, query, null, null, 0, 0, 0, false, null, Bytes.QUERYOPTION_OPLOGREPLAY);
}

void refreshTagList() {
String shardName = getReplSetNode().getShardName();
if (shardName == null)
return;

ListArea list = (ListArea) getBoundUnit(Item.tagList);
final DB db = ((RouterNode)getReplSetNode().getParentNode()).getMongo().getDB("config");
DBObject shard = db.getCollection("shards").findOne(new BasicDBObject("_id", shardName));
if (shard.containsField("tags")) {
BasicDBList tags = (BasicDBList) shard.get("tags");
if (tags.size() > 0) {
String[] array = new String[tags.size()];
int i = 0;
for (Object tag : tags) {
array[i++] = (String) tag;
}
list.items = array;
list.structureComponent();
return;
}
}
list.items = null;
list.structureComponent();
}

public void manageTags(ButtonBase button) {
FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.manageTags)).getDialog();
refreshTagList();
dialog.show();
}

public void addTag(ButtonBase button) {
final DB db = ((RouterNode)getReplSetNode().getParentNode()).getMongo().getDB("config");
final DBCollection col = db.getCollection("shards");
final String tag = getStringFieldValue(Item.atTag);
final DBObject query = new BasicDBObject("_id", getReplSetNode().getShardName());
final DBObject update = new BasicDBObject("$addToSet", new BasicDBObject("tags", tag));

new DbJob() {

@Override
public Object doRun() {
return col.update(query, update);
}

@Override
public String getNS() {
return col.getFullName();
}

@Override
public String getShortName() {
return "Add Tag";
}

@Override
public DBObject getRoot(Object result) {
BasicDBObject obj = new BasicDBObject("query", query);
obj.put("update", update);
return obj;
}

@Override
public void wrapUp(Object res) {
super.wrapUp(res);
refreshTagList();
}
}.addJob();
}

public void removeTag(ButtonBase button) {
final DB db = ((RouterNode)getReplSetNode().getParentNode()).getMongo().getDB("config");
final DBCollection col = db.getCollection("shards");
final String tag = getComponentStringFieldValue(Item.tagList);
if (tag == null)
return;

final DBObject query = new BasicDBObject("_id", getReplSetNode().getShardName());
final DBObject update = new BasicDBObject("$pull", new BasicDBObject("tags", tag));

new DbJob() {

@Override
public Object doRun() {
return col.update(query, update);
}

@Override
public String getNS() {
return col.getFullName();
}

@Override
public String getShortName() {
return "Remove Tag";
}

@Override
public DBObject getRoot(Object result) {
BasicDBObject obj = new BasicDBObject("query", query);
obj.put("update", update);
return obj;
}

@Override
public void wrapUp(Object res) {
super.wrapUp(res);
refreshTagList();
}
}.addJob();
}
}
4 changes: 2 additions & 2 deletions src/com/edgytech/umongo/RouterNode.java
Expand Up @@ -55,7 +55,7 @@ protected void populateChildren() {
for (Object obj : shards) {
try {
DBObject shard = (DBObject) obj;
String id = (String) shard.get("_id");
String shardName = (String) shard.get("_id");
String hosts = (String) shard.get("host");
String repl = null;
int slash = hosts.indexOf('/');
Expand All @@ -76,7 +76,7 @@ protected void populateChildren() {
}

if (repl != null || addrs.size() > 1) {
addChild(new ReplSetNode(id + " (" + repl + ")", addrs, mongo.getMongoOptions(), true));
addChild(new ReplSetNode(repl, addrs, mongo.getMongoOptions(), shardName));
} else {
addChild(new ServerNode(addrs.get(0), mongo.getMongoOptions(), false, false));
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/edgytech/umongo/ServerNode.java
Expand Up @@ -108,7 +108,7 @@ protected void populateChildren() {
@Override
protected void updateNode() {
if (isConfig)
label = "ConfigDB";
label = "Config MongoD";
else
label = "MongoD";
label += ": " + (host != null ? host : serverAddress.toString());
Expand Down

0 comments on commit 55d8904

Please sign in to comment.