-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
executable file
·178 lines (159 loc) · 6.28 KB
/
View.java
File metadata and controls
executable file
·178 lines (159 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.*;
public class View implements Serializable{
private static final long serialVersionUID = 1L;
private static final long maxPropatime = (long)(Math.log(My_Server.data_copies_num+1)/Math.log(2) * 500 * 2);
public ConcurrentHashMap<String, ViewItem> viewtable = new ConcurrentHashMap<String, ViewItem>();
//provide a list of "up" servers to assign copies of data to a new server if one of the original server goes down
public ArrayList<String> availableNodes(int number, ArrayList<String> now){
Iterator<String> it = viewtable.keySet().iterator();
ArrayList<String> result = new ArrayList<String>();
while(it.hasNext() && result.size() < number){
String key = (String) it.next();
boolean redundant = false;
if (now !=null){
for(String s:now){
if(key.equals(s))
redundant = true;
}
}
if(!redundant && viewtable.get(key).status.equals("up"))
result.add(key);
}
return result;
}
//garbage collect on view table which is run in background thread every 10 seconds
public void garbageCollect(){
Iterator<String> it = viewtable.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
//check if a server haven't been touched for longer than 2 minutes
if(System.currentTimeMillis() > viewtable.get(key).time + 2 * 60 * 1000 && viewtable.get(key).status.equals("up"))
viewtable.put(key, new ViewItem(System.currentTimeMillis(), "down"));
//check if a server is down for longer than max propagation time
long deathtime = viewtable.get(key).time + maxPropatime;
if(System.currentTimeMillis() > deathtime && viewtable.get(key).status.equals("down"))
viewtable.remove(key);
}
}
// gossip with server
public void gossipServer(String SvrID) throws InterruptedException, ClassNotFoundException, IOException{
View partnerView = My_Server.rpc.exchangeViews(this, SvrID);
mergeView(partnerView);
}
//merge view with other server
public void mergeView(View partner){
if(partner==null)return ;
Iterator<String> it = partner.viewtable.keySet().iterator();
while(it.hasNext()){
String key = (String)it.next();
ViewItem partneritem = partner.viewtable.get(key);
if(viewtable.containsKey(key)){
ViewItem localitem = viewtable.get(key);
if(localitem.time < partneritem.time)
viewtable.put(key, partneritem);
}else if(partneritem.status.equals("up"))
viewtable.put(key, partneritem);
}
}
//update view
public void updateView (String SvrID,String status){
ViewItem item = viewtable.get(SvrID);
item.status = status;
item.time = System.currentTimeMillis();
}
//gossip with simpleDB
public void gossipDB() throws InterruptedException {
AmazonSimpleDB sdb = new AmazonSimpleDBClient(basicAWSCredentials);
Region usEast1 = Region.getRegion(Regions.US_EAST_1);
sdb.setRegion(usEast1);
String domain = "ServerView";
try{
String qry = "select * from `" + domain + "`";
SelectRequest selectRequest = new SelectRequest(qry);
List<ReplaceableItem> updatedata = new ArrayList<ReplaceableItem>();
Set<String> localkeys = new HashSet<String>(viewtable.keySet());
for (Item item : sdb.select(selectRequest).getItems()) {
String svrID = item.getName();
List<Attribute> attrs = item.getAttributes();
long dbtime = 0;
String dbstat = "";
for (Attribute at: attrs){
switch(at.getName()){
case "time": dbtime = Long.parseLong(at.getValue());
case "status": dbstat = at.getValue();
}
}
//check if a server is down for longer than max propagation time
long deathtime = dbtime + maxPropatime;
if(System.currentTimeMillis() > deathtime && dbstat.equals("down")){
System.out.println("delete item!!!");
sdb.deleteAttributes(new DeleteAttributesRequest(domain, svrID));
viewtable.remove(svrID);
localkeys.remove(svrID);
continue;
}
//check if a server haven't been touched for 2 minutes
if(System.currentTimeMillis() > dbtime + 2 * 60 * 1000 && dbstat.equals("up")){
viewtable.put(svrID, new ViewItem(System.currentTimeMillis(), "down"));
localkeys.add(svrID);
}
//compare simpleDB with local view table and merge together
if(viewtable.containsKey(svrID)){
ViewItem v = viewtable.get(svrID);
if(dbtime > v.time)
viewtable.put(svrID, new ViewItem(dbtime, dbstat));
else{
updatedata.add(new ReplaceableItem().withName(svrID).withAttributes(
new ReplaceableAttribute().withName("status").withValue(v.status).withReplace(true),
new ReplaceableAttribute().withName("time").withValue(Long.toString(v.time)).withReplace(true)));
}
localkeys.remove(svrID);
}
else if(dbstat.equals("up"))
viewtable.put(svrID, new ViewItem(dbtime, dbstat));
}
//update local entries to simpleDB if simpleDB doesn't contain those entries
for(String s: localkeys){
ViewItem v = viewtable.get(s);
if(v.status.equals("up")){
updatedata.add(new ReplaceableItem().withName(s).withAttributes(
new ReplaceableAttribute().withName("status").withValue(v.status).withReplace(true),
new ReplaceableAttribute().withName("time").withValue(Long.toString(v.time)).withReplace(true)));
}
}
if(updatedata.size() > 0)
sdb.batchPutAttributes(new BatchPutAttributesRequest(domain, updatedata));
}
catch (AmazonServiceException ase){
System.out.println(ase);
}
catch (AmazonClientException ace){
System.out.println(ace);
}
}
public String toString(){
String ret = "<table > <caption> View Table </caption>";
Iterator<String> it = viewtable.keySet().iterator();
while(it.hasNext() ){
ret+= "<tr>";
String key = (String) it.next();
ViewItem item=viewtable.get(key);
ret+= "<td>"+key+"</td>";
ret+="<td>"+item.status+"</td>";
ret+="<td>"+item.time+"</td>";
ret+="</tr>";
}
return ret;
}
}