func (this *DefaultEngine) Heart(method string, parms map[string]string, body []byte) (map[string]string, error) { dtype, hastype := parms["type"] res := make(map[string]string) if !hastype { return nil, fmt.Errorf("parms error") } switch dtype { case "alive": res["status"] = "alive" case "addindex": this.Logger.Info("[INFO] body %v", string(body)) var nodeindex utils.NodeIndex if err := json.Unmarshal(body, &nodeindex); err != nil { this.Logger.Error("[ERROR] nodeindex json error %v", err) } this.Logger.Info("[INFO] nodeindex %v", nodeindex) nodeindex.Shard = make([]uint64, 0) local := fmt.Sprintf("%v:%v", this.LocalIP, this.LocalPort) for k, v := range nodeindex.ShardNodes { for _, s := range v { if local == s { indexname := fmt.Sprintf("%v_%v", nodeindex.IndexName, k) err := this.createIndex(indexname, utils.IDX_ROOT_PATH, nodeindex.IndexMapping) if err != nil { this.Logger.Error("[ERROR] create index error %v", err) res["status"] = err.Error() return res, nil } nodeindex.Shard = append(nodeindex.Shard, uint64(k)) go this.pullDetail(nodeindex.IndexName, k) } } } this.idxNodes[nodeindex.IndexName] = nodeindex res["status"] = "add index Success" default: res["status"] = "alive" } return res, nil }
func (this *Dispatcher) CreateIndex(method string, parms map[string]string, body []byte) error { var idxstruct utils.IndexStrct if err := json.Unmarshal(body, &idxstruct); err != nil { this.Logger.Error("[ERROR] json error %v", err) return err } if _, ok := this.IndexInfo[idxstruct.IndexName]; ok { this.Logger.Error("[ERROR] index [%v] already has ", idxstruct.IndexName) return fmt.Errorf("[ERROR] index [%v] already has ", idxstruct.IndexName) } sis := make([]ShardInfo, 0) seeds := make([]bool, len(this.NodeInfos)) if len(this.NodeInfos) < int(idxstruct.ShardNum) { this.Logger.Error("[ERROR] ShardNum[%v] is to large", idxstruct.ShardNum) return fmt.Errorf("[ERROR] ShardNum[%v] is to large", idxstruct.ShardNum) } var nodeinfo utils.NodeIndex nodeinfo.IndexName = idxstruct.IndexName nodeinfo.ShardNum = idxstruct.ShardNum nodeinfo.IndexMapping = idxstruct.IndexMapping nodeinfo.ShardNodes = make(map[uint64][]string) //nodeinfo.Shard = make([]uint64, idxstruct.ShardNum) for i := uint64(0); i < idxstruct.ShardNum; i++ { si := ShardInfo{ShardPathName: fmt.Sprintf("%v/%v_%v", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i), MaxOffset: 0, NodeInfos: make([]string, 0)} si.OffsetMmap, _ = utils.NewMmap(fmt.Sprintf("%v/%v_%v.offset", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i), utils.MODE_APPEND) si.DetailMmap, _ = utils.NewMmap(fmt.Sprintf("%v/%v_%v.dtl", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i), utils.MODE_APPEND) for { seed := rand.Intn(len(seeds)) if seeds[seed] == false { seeds[seed] = true net := make([]string, 0) si.NodeInfos = append(si.NodeInfos, fmt.Sprintf("%v:%v", this.NodeInfos[seed].Addr, this.NodeInfos[seed].MPort)) net = append(net, fmt.Sprintf("%v:%v", this.NodeInfos[seed].Addr, this.NodeInfos[seed].MPort)) nodeinfo.ShardNodes[uint64(seed)] = net this.Logger.Info("[INFO] NodeInfos::: %v", si.NodeInfos) break } } sis = append(sis, si) } info := idxInfo{Name: idxstruct.IndexName, ShardNum: idxstruct.ShardNum, MaxCnt: 0, IndexMapping: idxstruct.IndexMapping, ShardInfos: sis, ShardField: idxstruct.ShardField} this.IndexInfo[idxstruct.IndexName] = info for _, iinfo := range this.NodeInfos { iinfo.IdxChan <- nodeinfo } return this.storeStruct() }