func initStorageTester(server string, myhostport string) *StorageTester { tester := &StorageTester{} tester.myhostport = myhostport tester.recv_revoke = make(map[string]bool) tester.comp_revoke = make(map[string]bool) // Create RPC connection to storage server srv, err := rpc.DialHTTP("tcp", server) if err != nil { fmt.Printf("Could not connect to server %s, returning nil\n", server) return nil } // Listen cacherpc rpc.Register(cacherpc.NewCacheRPC(tester)) rpc.HandleHTTP() l, e := net.Listen("tcp", fmt.Sprintf(":%d", *portnum)) if e != nil { log.Fatal("listen error:", e) } go http.Serve(l, nil) tester.srv = srv return tester }
// iNewLibstore // parameters: // - server: master sever host port // - myhostport: local port to handle cache rpc // - flags: ALWAYS->always ask lease // return: // - pointer to new libstore // function: // - initialize libstore // - create connection to the master server // - get storage servers' infomation // - set up cachHandler, garbage collector timer and cache expiration timer func iNewLibstore(agencyid, server, myhostport string, flags int) (*Libstore, error) { // Initialize new libstore ls := &Libstore{} ls.flags = flags ls.myhostport = myhostport ls.cacheReqC = make(chan *CacheReq) // Create RPC connection to storage server cli, err := rpc.DialHTTP("tcp", server) if err != nil { return nil, err } // Get list of storage servers from master storage server args := storageproto.GetServersArgs{} var reply storageproto.RegisterReply // Try to connect to storage server for at most 5 times try := 5 sleepTime := 2000 for i := 0; ; i++ { err = cli.Call("StorageRPC.GetServers", args, &reply) if err == nil && reply.Ready { ls.cacheLock = new(sync.RWMutex) ls.cacheMap = make(map[string]*CacheEntry) ls.connLock = new(sync.Mutex) ls.connMap = make(map[int]*rpc.Client) ls.nodelist = reply.Servers crpc := cacherpc.NewCacheRPC(ls) rpc.Register(crpc) for i := 1; i < len(ls.nodelist); i++ { if server == ls.nodelist[i].HostPort { ls.connLock.Lock() ls.connMap[i] = cli ls.connLock.Unlock() break } } // succeeded go ls.cacheHandler() go ls.cacheTimer(2*storageproto.LEASE_SECONDS, GARBAGE_COLLECT) go ls.cacheTimer(storageproto.QUERY_CACHE_SECONDS, QUERY_COLLECT) return ls, nil } // failed if i == try-1 { if err != nil { return nil, err } return nil, lsplog.MakeErr("Storage system not ready") } time.Sleep(time.Duration(sleepTime) * time.Millisecond) } // Impossible to come here return ls, nil }