func (this *Services) Load() error { //attempt to load from datadir filename := fmt.Sprintf("%s/%s", this.DataDir, "services.json") bytes, err := ioutil.ReadFile(filename) if err != nil { return err } mp := dynmap.NewDynMap() err = mp.UnmarshalJSON(bytes) if err != nil { return err } for k, _ := range mp.Map { rt, ok := mp.GetDynMap(k) if !ok { continue } table, err := shards.ToRouterTable(rt) if err != nil { log.Println(err) continue } this.services[k] = table } return nil }
// Checkin to an entry. will update their router table if it is out of date. will update our router table if out of date. func EntryCheckin(routerTable *shards.RouterTable, entry *shards.RouterEntry) (*shards.RouterTable, bool, error) { // make sure our routertable is up to date. response, err := client.HttpApiCallSync( fmt.Sprintf("%s:%d", entry.Address, entry.HttpPort), cheshire.NewRequest(shards.CHECKIN, "GET"), 5*time.Second) if err != nil { return routerTable, false, fmt.Errorf("ERROR While contacting %s -- %s", entry.Address, err) } entry.LastSeenAt = time.Now() rev := response.MustInt64("rt_revision", 0) if rev == routerTable.Revision { return routerTable, false, nil } if rev < routerTable.Revision { //updating server. //set the new routertable. req := cheshire.NewRequest(shards.ROUTERTABLE_SET, "POST") req.Params().Put("router_table", req.ToDynMap()) response, err = client.HttpApiCallSync( fmt.Sprintf("%s:%d", entry.Address, entry.HttpPort), req, 5*time.Second) if err != nil { return routerTable, false, fmt.Errorf("ERROR While contacting for router table update %s -- %s", entry.Address, err) } if response.StatusCode() != 200 { return routerTable, false, fmt.Errorf("Error trying to Set router table %s -- %s", entry.Address, response.StatusMessage()) } } else { //updating local log.Printf("Found updated router table at: %s", entry.Address) //get the new routertable. response, err = client.HttpApiCallSync( fmt.Sprintf("%s:%d", entry.Address, entry.HttpPort), cheshire.NewRequest(shards.ROUTERTABLE_GET, "GET"), 5*time.Second) if err != nil { return routerTable, false, fmt.Errorf("ERROR While contacting %s -- %s", entry.Address, err) } mp, ok := response.GetDynMap("router_table") if !ok { return routerTable, false, fmt.Errorf("ERROR from %s -- BAD ROUTER TABLE RESPONSE %s", entry.Address, response) } rt, err := shards.ToRouterTable(mp) if err != nil { return routerTable, false, fmt.Errorf("ERROR While parsing router table %s -- %s", entry.Address, err) } log.Printf("SUCCESSFULLY update router table to revision %d", rt.Revision) routerTable = rt return routerTable, true, nil } return routerTable, false, nil }