Skip to content

Commit 3883c68

Browse files
author
Jose Angel Morena
committed
speed has been increased with preload
1 parent abb7ae5 commit 3883c68

23 files changed

+223
-146
lines changed

Diff for: .classpath

+9
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@
2323
<attribute name="maven.pomderived" value="true"/>
2424
</attributes>
2525
</classpathentry>
26+
<classpathentry kind="lib" path="C:/Users/jang3/.m2/repository/org/springframework/spring-context/4.3.12.RELEASE/spring-context-4.3.12.RELEASE.jar" sourcepath="C:/Users/jang3/.m2/repository/org/springframework/spring-context/4.3.12.RELEASE/spring-context-4.3.12.RELEASE-sources.jar">
27+
<attributes>
28+
<attribute name="maven.pomderived" value="true"/>
29+
<attribute name="maven.groupId" value="org.springframework"/>
30+
<attribute name="maven.artifactId" value="spring-context"/>
31+
<attribute name="maven.version" value="4.3.12.RELEASE"/>
32+
<attribute name="maven.scope" value="compile"/>
33+
</attributes>
34+
</classpathentry>
2635
<classpathentry kind="output" path="target/classes"/>
2736
</classpath>

Diff for: Configuration.xml

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ And last but not least, the configuration for each cluster is defined:
7070

7171
</info>
7272

73+

Diff for: Configuration2.xml

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!--
3-
The aim of this second xml file is to make clear that the configuration is possible to be modified
4-
in runtime by executing this rest call: http://localhost/changeXML?configFile=Configuration2.xml
5-
Then the program takes into account the new configuration, with less clients, more clusters and different weights.
6-
-->
2+
<!--
3+
There is only a single client, "clienteA". Which has 3 different devices, which are managed by different clusters.
4+
-->
75
<info>
8-
96
<account>
107
<type>clienteA</type>
118
<targetDevice>XBox</targetDevice>
129
<targetDevice>Panasonic</targetDevice>
1310
<targetDevice>osmf</targetDevice>
1411
</account>
15-
16-
12+
1713
<device>
1814
<type>XBox</type>
1915
<pluginVersion>3.3.1</pluginVersion>
@@ -22,7 +18,7 @@
2218

2319
<device>
2420
<type>Panasonic</type>
25-
<pluginVersion>3.3.5</pluginVersion>
21+
<pluginVersion>3.3.2</pluginVersion>
2622
<pingTime>5</pingTime>
2723
</device>
2824

@@ -33,27 +29,27 @@
3329
</device>
3430

3531
<cluster>
36-
<from>XBox</from>
32+
<from>Panasonic</from>
3733
<name>clusterA.com</name>
38-
<weight>3</weight>
34+
<weight>5</weight>
3935
</cluster>
4036

4137
<cluster>
42-
<from>XBox</from>
38+
<from>Panasonic</from>
4339
<name>clusterB.com</name>
44-
<weight>7</weight>
40+
<weight>5</weight>
4541
</cluster>
4642

4743
<cluster>
48-
<from>XBox</from>
44+
<from>Panasonic</from>
4945
<name>clusterC.com</name>
50-
<weight>7</weight>
46+
<weight>5</weight>
5147
</cluster>
5248

5349
<cluster>
54-
<from>Panasonic</from>
55-
<name>clusterA.com</name>
56-
<weight>1</weight>
50+
<from>XBox</from>
51+
<name>clusterB.com</name>
52+
<weight>10</weight>
5753
</cluster>
5854

5955
<cluster>
@@ -65,8 +61,9 @@
6561
<cluster>
6662
<from>osmf</from>
6763
<name>clusterB.com</name>
68-
<weight>7</weight>
64+
<weight>10</weight>
6965
</cluster>
7066

7167
</info>
7268

69+

Diff for: README.md

-11
This file was deleted.

Diff for: src/main/java/server/server/App.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package server.server;
2+
23
import org.springframework.boot.SpringApplication;
34
import org.springframework.boot.autoconfigure.SpringBootApplication;
45
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
56
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
67
import org.springframework.stereotype.Component;
78

9+
810
/*
911
* Programmer: Jose Angel Morena
1012
* MAIN APPLICATION
1113
*/
14+
1215
@SpringBootApplication
16+
@EnableCaching //enables Spring Caching functionality
1317
public class App {
18+
1419
public static void main(String[] args) {
1520
SpringApplication.run(App.class, args);
1621
}
@@ -23,6 +28,6 @@ public class CustomContainer implements EmbeddedServletContainerCustomizer {
2328
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
2429
configurableEmbeddedServletContainer.setPort(80);
2530
}
26-
2731
}
32+
2833
}

Diff for: src/main/java/server/server/Configuracio.java

-57
This file was deleted.

Diff for: src/main/java/server/server/Configuration.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package server.server;
2+
3+
import java.util.TreeMap;
4+
5+
/* Programmer: Jose Angel Morena
6+
* This class encapsulates the information of the configuration to be delivered to the client.
7+
* There is a Map to store the clusters because there may be more than one to handle the petition. The structure is a map because
8+
* the name of the cluster is associated to its weight.
9+
*/
10+
public class Configuration {
11+
12+
private Integer totalWeight;
13+
/*
14+
* This treemap is here for a single reason. It is needed to decide which cluster is going to be used, given a configuration.
15+
* It is going to be used in the algorithm of the roulette to distribute the load among clusters
16+
*/
17+
private TreeMap<Integer, String> clusters= new TreeMap<Integer, String>();
18+
private String pingTime;
19+
20+
21+
public Configuration() {
22+
//this.code= UUID.randomUUID().toString();; //generate unique code
23+
}
24+
25+
public TreeMap<Integer, String> getClusters() {
26+
return clusters;
27+
}
28+
/*
29+
public void addCluster(String cluster, int weight) {
30+
clusters.put(cluster, weight);
31+
}
32+
33+
public void setClusters(TreeMap<String, Integer> clusters) {
34+
this.clusters = clusters;
35+
}
36+
*/
37+
public String getPingTime() {
38+
return pingTime;
39+
}
40+
41+
public void setPingTime(String pingTime) {
42+
this.pingTime = pingTime;
43+
}
44+
45+
public String toString() {
46+
return "; pingTime: " + this.pingTime + "; clusters: " + this.clusters;
47+
48+
}
49+
50+
public Integer getTotalWeight() {
51+
return totalWeight;
52+
}
53+
54+
public void setTotalWeight(Integer totalWeight) {
55+
this.totalWeight = totalWeight;
56+
}
57+
58+
59+
}

Diff for: src/main/java/server/server/ServiceController.java

+36-42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package server.server;
22

33
import java.io.File;
4-
import java.util.Map.Entry;
4+
import java.util.Map;
55
import java.util.Random;
6-
import java.util.TreeMap;
6+
import java.util.UUID;
77

88
import org.springframework.boot.autoconfigure.web.ErrorController;
99
import org.springframework.http.HttpStatus;
@@ -21,10 +21,24 @@
2121
*/
2222
@RestController
2323
public class ServiceController implements ErrorController {
24-
/*
25-
* filename of configuration file
24+
25+
private Random seed = new Random();//random generator
26+
27+
/*This attribute is going to store all of the configuration. Its aim is to provide a preload because this way
28+
* the access to the data is going to be much quicker in the rest call
29+
*
30+
* I assumed per each technology there is a single version to avoid a one more hash entrance.
2631
*/
27-
private String configFile="Configuration.xml";
32+
private Map<String,Map<String,Configuration>> info;
33+
34+
private String configFile="Configuration.xml"; //filename of configuration file
35+
36+
//un cliente tiene varias plataformas
37+
//una plataforma UN UNICO PLUGIN,
38+
//plataforma y plugin sera la clave para entrar en PING TIME Y TREEMAP DE SERVERS
39+
public ServiceController() {
40+
info = XMLHandler.returnInfo(this.configFile); //extract data considering the xml configuration
41+
}
2842

2943
public String getConfigFile() {
3044
return configFile;
@@ -38,14 +52,16 @@ public void setConfigFile(String configFile) {
3852
* url example:
3953
* http://localhost/changeXML?configFile=Configuration.xml
4054
* http://localhost/changeXML?configFile=Configuration2.xml
55+
*
56+
* It is possible to modify the current xml configuration file and then by using the rest call reload that new configuration
4157
*/
4258
@RequestMapping(value = "/changeXML",params= {"configFile"},method = RequestMethod.GET)
4359
public ResponseEntity<?> updateConfigFile(@RequestParam("configFile") String file) {
4460

4561
File f = new File(file);
46-
System.out.println(f);
4762
if(f.exists() && !f.isDirectory()) { //file is updated if and only if it exisits
48-
this.configFile=file;
63+
this.configFile=file;
64+
info = XMLHandler.returnInfo(this.configFile); //extract data considering the xml configuration
4965
return new ResponseEntity<String>("XML configuration file has been updated to: "+file, HttpStatus.OK);
5066
}
5167
//otherwise path is not going to be updated
@@ -61,6 +77,11 @@ public ResponseEntity<?> updateConfigFile(@RequestParam("configFile") String fil
6177
* Afterwards, the cluster is chosen by the algorithm.
6278
* Eventually the xml is returned.
6379
*
80+
*
81+
* The return, chooses a random cluster following a uniform distribution, the reason behind is to distribute more uniformly
82+
the chosen cluster according to the clusters of that technology
83+
*
84+
*
6485
* url example:
6586
* http://localhost/getData?accountCode=clienteA&targetDevice=XBox&pluginVersion=3.3.1
6687
*/
@@ -71,43 +92,16 @@ public ResponseEntity<?> updateConfigFile(@RequestParam("configFile") String fil
7192
headers = "Accept=application/xml",
7293
method = RequestMethod.GET)
7394
public @ResponseBody ResponseEntity<?> platformPetition(@RequestParam("accountCode") String accountCode, @RequestParam("targetDevice") String targetDevice, @RequestParam("pluginVersion") String pluginVersion ) {
74-
75-
Configuracio config= XMLHandler.check_configuration(accountCode,targetDevice,pluginVersion,this.configFile); //extract data considering the xml configuration
76-
77-
if (config==null) return new ResponseEntity<String>("",HttpStatus.OK);
78-
79-
String cluster=chooseCluster(config);
80-
return new ResponseEntity<q>(new q(cluster,String.valueOf(config.getPingTime()),config.getCode()), HttpStatus.OK);
95+
try {
96+
Configuration config = info.get(accountCode).get(targetDevice+pluginVersion);
97+
return new ResponseEntity<q>(new q(config.getClusters().ceilingEntry(new Random().nextInt(config.getTotalWeight()))
98+
.getValue(),config.getPingTime(),new UUID(this.seed.nextLong(), this.seed.nextLong()).toString()), HttpStatus.OK);
99+
}
100+
catch (Exception e) {
101+
return null;
102+
}
81103
}
82-
83104

84-
/*
85-
* This methods decides statistically which cluster is to be returned.
86-
* Roulette wheel selection algorithm
87-
* There is a list of clusters stored in a structure. Then a random number is chosen between 0 and
88-
* the sum of all the weights. This random number is generated with an uniform distribution.
89-
* Search in the cluster list for the first server for which the sum of weights for all servers up
90-
* to and including this cluster is more than the random value.
91-
*
92-
* I decided to use this method for three reasons:
93-
* - Firstly, if the xml is modified in run time, the frequency of selections is going to be according
94-
* to the xml configuration. Also if some other clusters are added, these will be taken into consideration.
95-
* - Secondly, this approach allows to not count the number of requests processed in order to decide which cluster to return.
96-
* - Last but not least, I believe it to be a good way to distribute the load among the clusters.
97-
*/
98-
public String chooseCluster(Configuracio config) {
99-
TreeMap<Integer, String> pool= new TreeMap<Integer, String>();
100-
Integer totalWeight=0;
101-
for ( Entry<String, Integer> entry : config.getClusters().entrySet() ) {
102-
totalWeight += entry.getValue();
103-
pool.put(totalWeight, entry.getKey()); //associate each server with the sum of the weights so far
104-
}
105-
106-
int rnd = new Random().nextInt(totalWeight);
107-
108-
return pool.ceilingEntry(rnd).getValue();
109-
}
110-
111105
/*
112106
* error handler
113107
*/

0 commit comments

Comments
 (0)