Skip to content

Commit

Permalink
fleshed out some shard operations, working on reshard...
Browse files Browse the repository at this point in the history
  • Loading branch information
sehejsohal committed May 28, 2019
1 parent 5cf35fa commit e207d4f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 38 deletions.
2 changes: 1 addition & 1 deletion forwarding/forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"io/ioutil"

"github.com/gorilla/mux"
"github.com/mrhea/CMPS128_Assignment3/structs"
"github.com/mrhea/CMPS128_Assignment4/structs"

"log"
"net/http"
Expand Down
4 changes: 2 additions & 2 deletions gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"
"time"

"github.com/mrhea/CMPS128_Assignment3/structs"
"github.com/mrhea/CMPS128_Assignment3/view"
"github.com/mrhea/CMPS128_Assignment4/structs"
"github.com/mrhea/CMPS128_Assignment4/view"
)

// Query health check message to nodes.
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"os"

"github.com/mrhea/CMPS128_Assignment3/rest"
"github.com/mrhea/CMPS128_Assignment4/rest"
)

// func main() {
Expand Down Expand Up @@ -46,8 +46,11 @@ func main() {

viewString := os.Getenv("VIEW")

shardCount := os.Getenv("SHARD_COUNT")

log.Printf("Starting replica instance at IP: %s", owner)

// Initialize endpoints, database, and view
rest.InitServer(owner, viewString)
}

rest.InitServer(owner, viewString, shardCount)
}
16 changes: 11 additions & 5 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package rest
import (
"bytes"
"encoding/json"
"github.com/mrhea/CMPS128_Assignment4/shard"
"io/ioutil"
"log"
"net/http"
"strings"
"time"

"github.com/gorilla/mux"
gsp "github.com/mrhea/CMPS128_Assignment3/gossip"
"github.com/mrhea/CMPS128_Assignment3/kvs"
"github.com/mrhea/CMPS128_Assignment3/structs"
"github.com/mrhea/CMPS128_Assignment3/view"
gsp "github.com/mrhea/CMPS128_Assignment4/gossip"
"github.com/mrhea/CMPS128_Assignment4/kvs"
"github.com/mrhea/CMPS128_Assignment4/structs"
"github.com/mrhea/CMPS128_Assignment4/view"
)

//const NULL int = -999

type server struct {
db *kvs.Database
V *view.View
S *shard.ShardView
stalled []*kvs.Entry
}

Expand Down Expand Up @@ -598,7 +600,7 @@ func fetchEntries(w http.ResponseWriter, r *http.Request) {
}

// InitServer setups a RESTful-accessible API.
func InitServer(socket, viewString string) {
func InitServer(socket, viewString, shardCount string) {
log.Println("REST: Initializing a new server node")
// Init router
log.Println("REST: Initializing a new router")
Expand All @@ -608,6 +610,10 @@ func InitServer(socket, viewString string) {
log.Println("REST: Initializing VIEW for router")
node.V = view.InitView(socket, viewString)

// Init shards
log.Println("REST: Initializing SHARDS for router")
node.S = shard.InitShards(socket, shardCount, viewString)

// Init database
log.Println("REST: Initializing DATABASE for router")
node.db = kvs.InitDB()
Expand Down
96 changes: 92 additions & 4 deletions shard/shard.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,98 @@
package shard

import (

"log"
"os"
"strconv"
"strings"
)

type Shard struct {
ID int //data type subject to change
Members []string
type shard struct {
members []string
numKeys int //probably don't need this?
}

type ShardView struct {
id int //shard ID of current node...
shardDB []*shard
}
//Each Node has a shardView, where it can see all the shards, and the members of all the shards/
//It can also see it's own shardID, so we can access that data without a lookup.
func InitShards(owner, shardString, viewOfReplicas string) *ShardView {
shardCount, err := strconv.Atoi(shardString)
if err != nil{
panic(err)
}
var S ShardView
//S.shardDB = make(map[int]*shard)

replicas := strings.Split(viewOfReplicas, ",")
if 2*shardCount > len(replicas) { //check minimum length(each shard must have @ least 2)
log.Println("Shard count too small, ERROR") //throw an error here?
os.Exit(126)
}
//correct length, continue...
for i := 1; i <= shardCount; i++ {
if len(replicas) >= 2 {
var ip1 string
var ip2 string
ip1, ip2, replicas = replicas[0], replicas[1], replicas[2:]
temp := &shard{members:[], numKeys:0}
temp.members = append(temp.members, ip1)
temp.members = append(temp.members, ip2)
S.shardDB = append(S.shardDB, temp) //is this the right way of doing this?
if owner == ip1 || owner == ip2 {
S.id = i
}
} else if len(replicas) == 1{
ip3 := replicas[0]
temp := &S.shardDB[i-1].members
*temp = append(*temp, ip3)
if owner == ip3 {
S.id = i-1
}
}
}
//we are now correctly sharded in either groups of 2,
//with one group of 3 for odd lengths
//this can change, if we want larger default groups...
return &S
}

func Reshard(shardCount int, s *ShardView){
/*
How do we implement this? We'd have to decide which kvs values go where...
It'd probably be easiest to figure out which IPs aren't in any shards, and
append them one by one to the smallest shard. So:
1. Locate smallest shard
2. Append new IP
3. Copy all KVS
4. Repeat until all IP's are in a shard
(Don't delete this ^, add to mechanisms.txt)
*/
}

//gets all active shards in the form of an int list.
//easy to marshall into json data.
func GetAllShards(s *ShardView) []int {
shardIDs := make([]int, 0) //apparently if you make a slice like this, it outputs correctly to json?
//var shardIDs []int
for i := 1; i <= len(s.shardDB); i++{
if s.shardDB[i] != nil {
shardIDs = append(shardIDs, i)
}
}
return shardIDs
}

func GetCurrentShard(s *ShardView) int {
return s.id
}

func GetMembersOfShard(ID int, s *ShardView) []string {
return s.shardDB[ID].members
}

func GetNumKeys(s *ShardView) int {
//I'm not sure if I want to keep track of this data in the shard...
}
25 changes: 3 additions & 22 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,7 @@ type ViewDeleteError struct {
Message string `json:"message"`
}

// CausalPut response
// May be used for either forwarding metadata between
// replicas, or responding to a forward
type CausalPut struct {
type NodeShard struct {
Message string `json:"message"`
Version string `json:"version"`
}

// CausalGet response
type CausalGet struct {
Message string `json:"message"`
Version string `json:"version"`
Metadata string `json:"causal-metadata"`
Value string `json:"value"`
}

// CausalGetError response
// Used in case of GET request for key-value pair with a
// version number that does not yet exist.
type CausalGetError struct {
Error string `json:"message"`
Version string `json:"version"`
}
ID string `json:"shard-id"`
}
2 changes: 1 addition & 1 deletion view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type View struct {
}

// Replica holds the address of a replica.
// Used in: rest.go/PutView
// Used in: rest.go/PutViewv
type Replica struct {
Address string `json:"socket-address"` // the address of a replica
}
Expand Down

0 comments on commit e207d4f

Please sign in to comment.