Example #1
0
func (m *requestHandlerContext) createIndexRequest(w http.ResponseWriter, r *http.Request) {

	if !doAuth(r, w, m.clusterUrl) {
		return
	}

	// convert request
	request := m.convertIndexRequest(r)
	if request == nil {
		sendIndexResponseWithError(w, "Unable to convert request for create index")
		return
	}

	indexDefn := request.Index

	if indexDefn.DefnId == 0 {
		defnId, err := common.NewIndexDefnId()
		if err != nil {
			sendIndexResponseWithError(w, fmt.Sprintf("Fail to generate index definition id %v", err))
			return
		}
		indexDefn.DefnId = defnId
	}

	// call the index manager to handle the DDL
	logging.Debugf("RequestHandler::createIndexRequest: invoke IndexManager for create index bucket %s name %s",
		indexDefn.Bucket, indexDefn.Name)

	if err := m.mgr.HandleCreateIndexDDL(&indexDefn); err == nil {
		// No error, return success
		sendIndexResponse(w)
	} else {
		// report failure
		sendIndexResponseWithError(w, fmt.Sprintf("%v", err))
	}
}
Example #2
0
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
}
Example #3
0
func (o *MetadataProvider) CreateIndexWithPlan(
	name, bucket, using, exprType, partnExpr, whereExpr string,
	secExprs []string, isPrimary bool, plan map[string]interface{}) (c.IndexDefnId, error, bool) {

	// FindIndexByName will only return valid index
	if o.FindIndexByName(name, bucket) != nil {
		return c.IndexDefnId(0), errors.New(fmt.Sprintf("Index %s already exist.", name)), false
	}

	var deferred bool = false
	var wait bool = true
	var nodes []string = nil

	if plan != nil {
		logging.Debugf("MetadataProvider:CreateIndexWithPlan(): plan %v", plan)

		ns, ok := plan["nodes"].([]interface{})
		if ok {
			if len(ns) != 1 {
				return c.IndexDefnId(0), errors.New("Create Index is allowed for one and only one node"), false
			}
			n, ok := ns[0].(string)
			if ok {
				nodes = []string{n}
			} else {
				return c.IndexDefnId(0),
					errors.New(fmt.Sprintf("Fails to create index.  Node '%v' is not valid", plan["nodes"])),
					false
			}
		} else {
			n, ok := plan["nodes"].(string)
			if ok {
				nodes = []string{n}
			} else if _, ok := plan["nodes"]; ok {
				return c.IndexDefnId(0),
					errors.New(fmt.Sprintf("Fails to create index.  Node '%v' is not valid", plan["nodes"])),
					false
			}
		}

		deferred2, ok := plan["defer_build"].(bool)
		if !ok {
			deferred_str, ok := plan["defer_build"].(string)
			if ok {
				var err error
				deferred2, err = strconv.ParseBool(deferred_str)
				if err != nil {
					return c.IndexDefnId(0),
						errors.New("Fails to create index.  Parameter defer_build must be a boolean value of (true or false)."),
						false
				}
				deferred = deferred2
				wait = !deferred

			} else if _, ok := plan["defer_build"]; ok {
				return c.IndexDefnId(0),
					errors.New("Fails to create index.  Parameter defer_build must be a boolean value of (true or false)."),
					false
			}
		} else {
			deferred = deferred2
			wait = !deferred
		}
	}

	logging.Debugf("MetadataProvider:CreateIndex(): deferred_build %v sync %v nodes %v", deferred, wait, nodes)

	watcher, err, retry := o.findWatcherWithRetry(nodes)
	if err != nil {
		return c.IndexDefnId(0), err, retry
	}

	// set the node list using indexerId
	nodes = []string{string(watcher.getIndexerId())}

	defnID, err := c.NewIndexDefnId()
	if err != nil {
		return c.IndexDefnId(0),
			errors.New(fmt.Sprintf("Fails to create index. Fail to create uuid for index definition.")),
			false
	}

	idxDefn := &c.IndexDefn{
		DefnId:          defnID,
		Name:            name,
		Using:           c.IndexType(using),
		Bucket:          bucket,
		IsPrimary:       isPrimary,
		SecExprs:        secExprs,
		ExprType:        c.ExprType(exprType),
		PartitionScheme: c.SINGLE,
		PartitionKey:    partnExpr,
		WhereExpr:       whereExpr,
		Deferred:        deferred,
		Nodes:           nodes}

	content, err := c.MarshallIndexDefn(idxDefn)
	if err != nil {
		return 0, err, false
	}

	key := fmt.Sprintf("%d", defnID)
	if _, err = watcher.makeRequest(OPCODE_CREATE_INDEX, key, content); err != nil {
		return defnID, err, false
	}

	if wait {
		err := watcher.waitForEvent(defnID, []c.IndexState{c.INDEX_STATE_ACTIVE, c.INDEX_STATE_DELETED})
		return defnID, err, false
	}

	return defnID, nil, false
}