Skip to content

Commit

Permalink
Merge pull request #16 from Saetch/3-dynamically_increase_map_size_wh…
Browse files Browse the repository at this point in the history
…en_new_values_are_added_for_nonexistent_points

3 dynamically increase map size when new values are added for nonexistent points
  • Loading branch information
Saetch authored May 26, 2024
2 parents 7f43dbd + 92d3b29 commit 700358e
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cluster/Master/Master.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<ItemGroup>
Expand Down
102 changes: 91 additions & 11 deletions Cluster/Master/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
using System.Reflection.Metadata.Ecma335;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using System.Text;
public class Program{

static int static_width_per_node = 2;
static int static_height_per_node = 2;

static int Main(String[] args){


Expand All @@ -27,16 +24,89 @@ static int Main(String[] args){

//configure the builder to accept external connections to the server ("0.0.0.0")
builder.WebHost.ConfigureKestrel(options => options.Listen(IPAddress.Any, 8080));


var serializerOptions = new JsonSerializerOptions
{
TypeInfoResolver = AppJsonSerializerContext.Default
};
var app = builder.Build();

app.MapGet("/", () => Results.BadRequest("No value provided. Please provide a value in the format 'x_y'"));
app.MapGet("/getValue/{values}", async (string values) =>
{
try {

var result = await getValue(values);
var result = await GetValue(values);
Console.WriteLine("Returning result: " + result);

return Results.Ok(result.Replace("\"", "'"));

} catch (Exception e) {
return Results.BadRequest(e.Message);
}

});
app.MapGet("/getValue/{x}/{y}", async (double x, double y) =>
{
try {

var result = await GetValue(""+x+"_"+y);
Console.WriteLine("Returning result: " + result);

return Results.Ok(result.Replace("\"", "'"));

} catch (Exception e) {
return Results.BadRequest(e.Message);
}

});
app.MapPost("/newValue/{x}/{y}/{value}", async (int x, int y, double value) =>
{
try {
XYValues addValue = new()
{
x = x,
y = y,
value = value
};
var result = await AddValue(addValue);
Console.WriteLine("Returning result: " + result);

return Results.Ok(result.Replace("\"", "'"));

} catch (Exception e) {
return Results.BadRequest(e.Message);
}

});
app.MapPost("/addValue/{x}/{y}/{value}", async (int x, int y, double value) =>
{
try {
XYValues addValue = new()
{
x = x,
y = y,
value = value
};
var result = await AddValue(addValue);
Console.WriteLine("Returning result: " + result);

return Results.Ok(result.Replace("\"", "'"));

} catch (Exception e) {
return Results.BadRequest(e.Message);
}

});
app.MapPost("/saveValue/{x}/{y}/{value}", async (int x, int y, double value) =>
{
try {
XYValues addValue = new()
{
x = x,
y = y,
value = value
};
var result = await AddValue(addValue);
Console.WriteLine("Returning result: " + result);

return Results.Ok(result.Replace("\"", "'"));
Expand All @@ -51,9 +121,20 @@ static int Main(String[] args){
return 0;
}

static async Task<String> AddValue(XYValues value){
using HttpClient httpClient = new HttpClient();
string json = JsonSerializer.Serialize(
value!, AppJsonSerializerContext.Default.XYValues);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("http://coordinator:8080/organize/add_value", content);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}



static async Task<String> getValue(String input){
static async Task<String> GetValue(String input){
var inputs = input.Split('_');
if (inputs.Length != 2)
throw new ArgumentException("Input must be in the format 'x_y'");
Expand All @@ -70,7 +151,6 @@ static async Task<String> getValue(String input){
}


//TODO: Implement the function that finds the correct node in the network for dynamic node distribution. This is just for a completely static cluster
static String find_correct_node(int x, int y){
using (HttpClient httpClient = new HttpClient())
{
Expand Down Expand Up @@ -143,7 +223,7 @@ class XYValues




[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(XYValues[]))]
[JsonSerializable(typeof(XYValues))]
[JsonSerializable(typeof(String))]
Expand Down
37 changes: 28 additions & 9 deletions Cluster/Node_cs/ApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int initializeConfigValues()
HttpClient client = new HttpClient();
var response = client.PostAsync("http://" + this.COORDINATOR_SERVICE_URL + ":"+ this.PORT+"/organize/register/"+this.hostname, null).Result;
DealWithResponse(response);
return 0; //TODO! Update this if the Node is configured correctly according to the response from the coordinator service
return 0;
}

public WebApplication setupServer(){
Expand Down Expand Up @@ -142,7 +142,22 @@ private void registerRequests(WebApplication app)
return Results.BadRequest(e.Message);
}
});

app.MapPost("/addValue/", (Position toAdd) =>
{
try {
Console.WriteLine("Received addValue-call with params: " + toAdd.x + " " + toAdd.y + " " + toAdd.value);
var key = new Tuple<int, int>(toAdd.x, toAdd.y);
if (savedValues.ContainsKey(new Tuple<int, int>(toAdd.x, toAdd.y))){
savedValues[key] = toAdd.value;
}else{
savedValues.Add(key, toAdd.value);
}

return Results.Ok("Value added: " + toAdd.x + " " + toAdd.y + " " + toAdd.value);
} catch (Exception e) {
return Results.BadRequest(e.Message);
}
});


}
Expand Down Expand Up @@ -182,14 +197,16 @@ private void DealWithResponse(HttpResponseMessage response){
savedValues.Add(new Tuple<int, int>(x,y), value);
}


Console.WriteLine("Finished computing the response from coordinator service ... ");
}

}


public class SavedValue{
public required Position Position { get; set; }
public class Position{
public int x { get; set; }
public int y { get; set; }

public double value { get; set; }
}

Expand All @@ -206,7 +223,7 @@ public class HashedPosition{
public ushort hash { get; set; }
}

public class Position{
public class Point{
public int x { get; set; }
public int y { get; set; }
}
Expand All @@ -219,9 +236,9 @@ public class NodeResponse{

[JsonSerializable(typeof(String))]
[JsonSerializable(typeof(List<XYValues>))]
[JsonSerializable(typeof(List<Position>))]
[JsonSerializable(typeof(Position[]))]
[JsonSerializable(typeof(Position))]
[JsonSerializable(typeof(List<Point>))]
[JsonSerializable(typeof(Point[]))]
[JsonSerializable(typeof(Point))]
[JsonSerializable(typeof(XYValues))]
[JsonSerializable(typeof(XYValues[]))]
[JsonSerializable(typeof(HashedPosition))]
Expand All @@ -230,6 +247,8 @@ public class NodeResponse{
[JsonSerializable(typeof(List<Tuple<int, int>>))]
[JsonSerializable(typeof(NodeResponse))]
[JsonSerializable(typeof(List<NodeResponse>))]
[JsonSerializable(typeof(Position))]
[JsonSerializable(typeof(List<Position>))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{

Expand Down
30 changes: 15 additions & 15 deletions Cluster/Node_cs/NodeBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static async Task<List<NodeResponse>> QueryAllNodesWithHashes(ApiConfig
//get the input parameters from the query parameters in ASP.NET
public static async Task<List<XYValues>> GetMultipleSavedValues(String query, ApiConfig config){
var retList = new List<XYValues>();
List<Position> positions = [];
List<Point> positions = [];

string[] positionString = query.Split(";");
foreach (string pos in positionString){
Expand All @@ -58,10 +58,10 @@ public static async Task<List<XYValues>> GetMultipleSavedValues(String query, Ap
}
int x = Int32.Parse(parts[0]);
int y = Int32.Parse(parts[1]);
positions.Add(new Position { x = x, y = y });
positions.Add(new Point { x = x, y = y });
}

foreach (Position pos in positions){
foreach (Point pos in positions){
Console.WriteLine("Try getting saved value for: " + pos.x + "/" + pos.y);

if (config.savedValues.ContainsKey(new Tuple<int, int>(pos.x, pos.y))){
Expand Down Expand Up @@ -116,10 +116,10 @@ private static async Task FillInValuesForNode(List<int> positionIndices, List<Ha
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Received response: " + responseBody);
//deserialize responseBody to List<XYValues>
List<SavedValue> responseValues = ParseSavedValuesFromResponse(responseBody);
foreach (SavedValue value in responseValues){
Console.WriteLine("Filling in value: " + value.value + " at position: " + value.Position.x + "/" + value.Position.y);
Position pos = value.Position;
List<Position> responseValues = ParseSavedValuesFromResponse(responseBody);
foreach (Position value in responseValues){
Console.WriteLine("Filling in value: " + value.value + " at position: " + value.x + "/" + value.y);
Point pos = new Point { x = value.x, y = value.y };
if (values[pos.x-starting_x][pos.y-starting_y] != null){
Console.WriteLine("Value already filled in, skipping ... Something must have gone wrong!!");
}
Expand Down Expand Up @@ -328,7 +328,7 @@ private static List<NodeResponse> ParseNodeResponsesFromResponse(String response
values[i] = new double?[4];
}
Console.WriteLine("Checking matrix values ... ");
List<Position> toFindList = [];
List<Point> toFindList = [];
for (int i = -1; i < 3; i++)
{
for (int j = -1; j < 3; j++)
Expand All @@ -337,7 +337,7 @@ private static List<NodeResponse> ParseNodeResponsesFromResponse(String response
if (config.savedValues.ContainsKey(tuple)){
values[i + 1][j + 1] = config.savedValues[tuple];
}else{
toFindList.Add(new Position { x = zeroed_actual_x + i, y = zeroed_actual_y + j });
toFindList.Add(new Point { x = zeroed_actual_x + i, y = zeroed_actual_y + j });
}
}
}
Expand Down Expand Up @@ -473,10 +473,10 @@ public static String GetHoldingNode(int x, int y, ApiConfig config){
internal static async Task<List<XYValues>> DeleteSavedValuesBelow(string hash, ApiConfig api){

//Get all possible hashes for the given points
List<Position> hashTasks = new List<Position>();
List<Point> hashTasks = new List<Point>();

foreach (Tuple<int, int> key in api.savedValues.Keys){
hashTasks.Add(new Position { x = key.Item1, y = key.Item2 });
hashTasks.Add(new Point { x = key.Item1, y = key.Item2 });
}
var result = await QueryHasherForPoints(hashTasks);

Expand Down Expand Up @@ -519,7 +519,7 @@ internal static async Task<List<XYValues>> DeleteSavedValuesBelow(string hash, A
}


private static async Task<List<HashedPosition>> QueryHasherForPoints(List<Position> positions){
private static async Task<List<HashedPosition>> QueryHasherForPoints(List<Point> positions){
Console.WriteLine("Querying hasher service for points ... ");
var options = new JsonSerializerOptions
{
Expand All @@ -542,14 +542,14 @@ private static async Task<List<HashedPosition>> QueryHasherForPoints(List<Positi
};
}

internal static List<SavedValue> ParseSavedValuesFromResponse(String responseBody){
internal static List<Position> ParseSavedValuesFromResponse(String responseBody){
if (responseBody == null){
throw new Exception("Response body is null");
}
if (responseBody == "" || responseBody == "[]"){
return [];
}
List<SavedValue> ret = [];
List<Position> ret = [];
string[] objects = responseBody.Replace("[{","").Replace("}]","").Split("},{");
foreach (string obj in objects){
string[] parts = obj.Split(",");
Expand All @@ -562,7 +562,7 @@ internal static List<SavedValue> ParseSavedValuesFromResponse(String responseBod
int x = Int32.Parse(xInputs[1]);
int y = Int32.Parse(yInputs[1]);
double value = Double.Parse(valueInputs[1]);
ret.Add(new SavedValue { Position = new Position { x = x, y = y }, value = value });
ret.Add(new Position { x = x, y = y, value = value });
}
return ret;
}
Expand Down
6 changes: 3 additions & 3 deletions Cluster/coordinator/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub enum NodeCommand{

#[derive(Debug, Serialize, Clone, Copy, Deserialize)]
pub struct Position{
pub(crate) x: i32,
pub(crate) y: i32,
pub(crate) value: f64,
pub x: i32,
pub y: i32,
pub value: f64,
}


Expand Down
2 changes: 1 addition & 1 deletion Cluster/coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ async fn main() -> std::io::Result<()> {
.route("/get_complete_state", web::get().to(get_requests::get_complete_state))
.route("/get_node/{x}/{y}", web::get().to(get_requests::get_node_for_point))
.route("/getNode/{x}/{y}", web::get().to(get_requests::get_node_for_point))
.route("/upload_value/{x}/{y}/{value}", web::post().to(post_requests::upload_value))
.route("/hey", web::get().to(manual_hello))
.route("/debug_distribution", actix_web::web::get().to(get_requests::debug_distribution))
.route("/get_all_nodes", actix_web::web::get().to(get_requests::get_all_nodes))
.route("/add_value", actix_web::web::post().to(post_requests::upload_value))

)
.route("/ping", web::get().to(ping))
Expand Down
Loading

0 comments on commit 700358e

Please sign in to comment.