func (m *requestHandlerContext) makeCreateIndexRequest(defn common.IndexDefn, host string) bool { id, err := common.NewIndexDefnId() if err != nil { logging.Debugf("requestHandler.makeCreateIndexRequest(): fail to generate index definition id %v", err) return false } defn.DefnId = id // deferred build for restore defn.Deferred = true req := IndexRequest{Version: uint64(1), Type: CREATE, Index: defn} body, err := json.Marshal(&req) if err != nil { logging.Debugf("requestHandler.makeCreateIndexRequest(): cannot marshall create index request %v", err) return false } bodybuf := bytes.NewBuffer(body) resp, err := postWithAuth(host+"/createIndex", "application/json", bodybuf) if err != nil { logging.Debugf("requestHandler.makeCreateIndexRequest(): create index request fails %v", err) return false } response := new(IndexResponse) status := convertResponse(resp, response) if status == RESP_ERROR || response.Code == RESP_ERROR { logging.Debugf("requestHandler.makeCreateIndexRequest(): create index request fails") return false } return true }
func (m *LifecycleMgr) CreateIndex(defn *common.IndexDefn) error { if !defn.Deferred && !m.canBuildIndex(defn.Bucket) { logging.Errorf("LifecycleMgr.handleCreateIndex() : Cannot create index %s.%s while another index is being built", defn.Bucket, defn.Name) return errors.New(fmt.Sprintf("Cannot create Index %s.%s while another index is being built.", defn.Bucket, defn.Name)) } existDefn, err := m.repo.GetIndexDefnByName(defn.Bucket, defn.Name) if err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : createIndex fails. Reason = %v", err) return err } if existDefn != nil { topology, err := m.repo.GetTopologyByBucket(existDefn.Bucket) if err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : fails to find index instance. Reason = %v", err) return err } state, _ := topology.GetStatusByDefn(existDefn.DefnId) if state != common.INDEX_STATE_NIL && state != common.INDEX_STATE_DELETED { return errors.New(fmt.Sprintf("Index %s.%s already exist", defn.Bucket, defn.Name)) } } // Fetch bucket UUID. This confirms that the bucket has existed, but it cannot confirm if the bucket // is still existing in the cluster (due to race condition or network partitioned). bucketUUID, err := m.verifyBucket(defn.Bucket) if err != nil || bucketUUID == common.BUCKET_UUID_NIL { return errors.New("Bucket does not exist or temporarily unavaible for creating new index." + " Please retry the operation at a later time.") } defn.BucketUUID = bucketUUID if err := m.repo.CreateIndex(defn); err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : createIndex fails. Reason = %v", err) return err } if err := m.repo.addIndexToTopology(defn, common.IndexInstId(defn.DefnId)); err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : createIndex fails. Reason = %v", err) m.repo.DropIndexById(defn.DefnId) return err } if m.notifier != nil { if err := m.notifier.OnIndexCreate(defn); err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : createIndex fails. Reason = %v", err) m.repo.DropIndexById(defn.DefnId) m.repo.deleteIndexFromTopology(defn.Bucket, defn.DefnId) return err } } if err := m.updateIndexState(defn.Bucket, defn.DefnId, common.INDEX_STATE_READY); err != nil { logging.Errorf("LifecycleMgr.handleCreateIndex() : createIndex fails. Reason = %v", err) if m.notifier != nil { m.notifier.OnIndexDelete(defn.DefnId, defn.Bucket) } m.repo.DropIndexById(defn.DefnId) m.repo.deleteIndexFromTopology(defn.Bucket, defn.DefnId) return err } if !defn.Deferred { if m.notifier != nil { logging.Debugf("LifecycleMgr.handleCreateIndex() : start Index Build") if errMap := m.notifier.OnIndexBuild([]common.IndexDefnId{defn.DefnId}, []string{defn.Bucket}); len(errMap) != 0 { err := errMap[common.IndexInstId(defn.DefnId)] logging.Errorf("LifecycleMgr.hanaleCreateIndex() : createIndex fails. Reason = %v", err) m.notifier.OnIndexDelete(defn.DefnId, defn.Bucket) m.repo.DropIndexById(defn.DefnId) m.repo.deleteIndexFromTopology(defn.Bucket, defn.DefnId) return err } } } logging.Debugf("LifecycleMgr.handleCreateIndex() : createIndex completes") return nil }