func NewStartRequest(path string, dir string, args []string, allocated resource.ComputeResource, envs []string, host string, port int32) *cmd.ControlMessage { request := &cmd.ControlMessage{ Type: cmd.ControlMessage_StartRequest.Enum(), StartRequest: &cmd.StartRequest{ Path: proto.String(path), Args: args, Dir: proto.String(dir), Resource: &cmd.ComputeResource{ CpuCount: proto.Int32(int32(allocated.CPUCount)), CpuLevel: proto.Int32(int32(allocated.CPULevel)), Memory: proto.Int32(int32(allocated.MemoryMB)), }, Envs: envs, Host: proto.String(host), Port: proto.Int32(port), HashCode: proto.Uint32(0), }, } // generate a unique hash code for the request data, err := proto.Marshal(request) if err != nil { log.Fatalf("marshaling start request error: %v", err) return nil } request.StartRequest.HashCode = proto.Uint32(uint32(util.Hash(data))) return request }
func HashByKey(input reflect.Value, shard int) int { v := guessKey(input) dt := v.Type() if dt.Kind() == reflect.Interface { dt = reflect.TypeOf(v.Interface()) v = reflect.ValueOf(v.Interface()) } var x int switch dt.Kind() { case reflect.Int: x = int(v.Int()) % shard case reflect.String: x = int(util.Hash([]byte(v.String()))) % shard case reflect.Slice: x = int(util.Hash(v.Bytes())) % shard default: println("unexpected key to hash:", v.Kind().String()) } return x }
func HashByKey(input reflect.Value, shard int) int { v := guessKey(input) dt := v.Type() if dt.Kind() == reflect.Interface { dt = reflect.TypeOf(v.Interface()) v = reflect.ValueOf(v.Interface()) } var x int switch dt.Kind() { case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8: x = int(v.Int()) % shard case reflect.String: x = int(util.Hash([]byte(v.String()))) % shard case reflect.Slice: x = int(util.Hash(v.Bytes())) % shard default: log.Fatalf("unexpected key to hash %s: %v", v.Kind().String(), v) } return x }