Exemplo n.º 1
0
// NewEventSubSocketHandler returns a function that handles event notifications
func NewEventSubSocketHandler(eventSubSocket *zmq4.Socket) func(zmq4.State) error {
	var nodeIDMap map[string]uint32
	var err error

	if nodeIDMap, err = centraldb.GetNodeIDMap(); err != nil {
		fog.Critical("centraldb.GetNodeIDMap() failed %s", err)
	}

	return func(_ zmq4.State) error {
		var err error
		var ok bool
		var webWriterStart msg.WebWriterStart
		var timestamp time.Time
		var sourceNodeID uint32

		marshalledMessage, err := eventSubSocket.RecvMessage(0)
		if err != nil {
			return fmt.Errorf("RecvMessage %s", err)
		}

		// the 0th part should be the topic, we skip that

		err = json.Unmarshal([]byte(marshalledMessage[1]), &webWriterStart)
		if err != nil {
			return fmt.Errorf("Unmarshal %s", err)
		}

		if webWriterStart.MessageType != "web-writer-start" {
			return fmt.Errorf("unknown message type '%s'",
				webWriterStart.MessageType)
		}

		timestamp, err = tools.ParseTimestampRepr(webWriterStart.TimestampRepr)
		if err != nil {
			return fmt.Errorf("unable to parse %s %s",
				webWriterStart.TimestampRepr, err)
		}

		sourceNodeID, ok = nodeIDMap[webWriterStart.SourceNodeName]
		if !ok {
			return fmt.Errorf("unknown source_node_name %s",
				webWriterStart.SourceNodeName)
		}

		fog.Debug("cancel-segments-from-node %s", webWriterStart.SourceNodeName)

		// cancel all all segment rows
		//    * from a specifiic source node
		//    * are in active status
		//    * with a timestamp earlier than the specified time.
		// This is triggered by a web server restart
		stmt := nodedb.Stmts["cancel-segments-from-node"]
		if _, err = stmt.Exec(sourceNodeID, timestamp); err != nil {
			return fmt.Errorf("cancel-segments-from-node %s", err)
		}

		return nil
	}
}
Exemplo n.º 2
0
// NewNimbusioWriter returns an entity that implements the NimbusioWriter interface
func NewNimbusioWriter() (NimbusioWriter, error) {
	var err error
	var state writerState
	state.SegmentMap = make(map[segmentKey]segmentMapEntry)

	if state.NodeIDMap, err = centraldb.GetNodeIDMap(); err != nil {
		return nil, fmt.Errorf("centraldb.GetNodeIDMap() failed %s", err)
	}

	if state.FileSpaceInfo, err = tools.NewFileSpaceInfo(nodedb.NodeDB); err != nil {
		return nil, err
	}

	if state.ValueFile, err = NewOutputValueFile(state.FileSpaceInfo); err != nil {
		return nil, err
	}

	writerChan := make(chan interface{}, writerChanCapacity)
	state.WriterChan = nimbusioWriterChan(writerChan)

	if state.StatGrabber, err = gostatgrabber.NewStatGrabber(); err != nil {
		return nil, fmt.Errorf("NewStatGrabber failed %s", err)
	}

	startSyncTimer(&state)

	go func() {
		for item := range writerChan {
			switch request := item.(type) {
			case requestSync:
				handleSync(&state)
			case requestStartSegment:
				handleStartSegment(&state, request)
			case requestStoreSequence:
				handleStoreSequence(&state, request)
			case requestCancelSegment:
				handleCancelSegment(&state, request)
			case requestWaitSyncForFinishSegment:
				handleWaitSyncForFinishSegment(&state, request)
			case requestFinishSegment:
				handleFinishSegment(&state, request)
			case requestDestroyKey:
				handleDestroyKey(&state, request)
			case requestStartConjoinedArchive:
				handleStartConjoinedArchive(&state, request)
			case requestAbortConjoinedArchive:
				handleAbortConjoinedArchive(&state, request)
			case requestFinishConjoinedArchive:
				handleFinishConjoinedArchive(&state, request)
			default:
				fog.Error("unknown request type %T", item)
			}
		}
	}()

	return state.WriterChan, nil
}