func (db *SQLDB) ClaimActualLRP(logger lager.Logger, processGuid string, index int32, instanceKey *models.ActualLRPInstanceKey) (*models.ActualLRPGroup, *models.ActualLRPGroup, error) { logger = logger.WithData(lager.Data{"process_guid": processGuid, "index": index, "instance_key": instanceKey}) logger.Info("starting") defer logger.Info("complete") var beforeActualLRP models.ActualLRP var actualLRP *models.ActualLRP err := db.transact(logger, func(logger lager.Logger, tx *sql.Tx) error { var err error actualLRP, err = db.fetchActualLRPForUpdate(logger, processGuid, index, false, tx) if err != nil { logger.Error("failed-fetching-actual-lrp-for-share", err) return err } beforeActualLRP = *actualLRP if !actualLRP.AllowsTransitionTo(&actualLRP.ActualLRPKey, instanceKey, models.ActualLRPStateClaimed) { logger.Error("cannot-transition-to-claimed", nil, lager.Data{"from_state": actualLRP.State, "same_instance_key": actualLRP.ActualLRPInstanceKey.Equal(instanceKey)}) return models.ErrActualLRPCannotBeClaimed } if actualLRP.State == models.ActualLRPStateClaimed && actualLRP.ActualLRPInstanceKey.Equal(instanceKey) { return nil } actualLRP.ModificationTag.Increment() actualLRP.State = models.ActualLRPStateClaimed actualLRP.ActualLRPInstanceKey = *instanceKey actualLRP.PlacementError = "" actualLRP.ActualLRPNetInfo = models.ActualLRPNetInfo{} actualLRP.Since = db.clock.Now().UnixNano() _, err = db.update(logger, tx, actualLRPsTable, SQLAttributes{ "state": actualLRP.State, "cell_id": actualLRP.CellId, "instance_guid": actualLRP.InstanceGuid, "modification_tag_index": actualLRP.ModificationTag.Index, "placement_error": actualLRP.PlacementError, "since": actualLRP.Since, "net_info": []byte{}, }, "process_guid = ? AND instance_index = ? AND evacuating = ?", processGuid, index, false, ) if err != nil { logger.Error("failed-claiming-actual-lrp", err) return db.convertSQLError(err) } return nil }) return &models.ActualLRPGroup{Instance: &beforeActualLRP}, &models.ActualLRPGroup{Instance: actualLRP}, err }
func (db *SQLDB) CrashActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, crashReason string) (*models.ActualLRPGroup, *models.ActualLRPGroup, bool, error) { logger = logger.WithData(lager.Data{"key": key, "instance_key": instanceKey, "crash_reason": crashReason}) logger.Info("starting") defer logger.Info("complete") var immediateRestart = false var beforeActualLRP models.ActualLRP var actualLRP *models.ActualLRP err := db.transact(logger, func(logger lager.Logger, tx *sql.Tx) error { var err error actualLRP, err = db.fetchActualLRPForUpdate(logger, key.ProcessGuid, key.Index, false, tx) if err != nil { logger.Error("failed-to-get-actual-lrp", err) return err } beforeActualLRP = *actualLRP latestChangeTime := time.Duration(db.clock.Now().UnixNano() - actualLRP.Since) var newCrashCount int32 if latestChangeTime > models.CrashResetTimeout && actualLRP.State == models.ActualLRPStateRunning { newCrashCount = 1 } else { newCrashCount = actualLRP.CrashCount + 1 } if !actualLRP.AllowsTransitionTo(&actualLRP.ActualLRPKey, instanceKey, models.ActualLRPStateCrashed) { logger.Error("failed-to-transition-to-crashed", nil, lager.Data{"from_state": actualLRP.State, "same_instance_key": actualLRP.ActualLRPInstanceKey.Equal(instanceKey)}) return models.ErrActualLRPCannotBeCrashed } actualLRP.ModificationTag.Increment() actualLRP.State = models.ActualLRPStateCrashed actualLRP.ActualLRPInstanceKey.InstanceGuid = "" actualLRP.ActualLRPInstanceKey.CellId = "" actualLRP.ActualLRPNetInfo = models.ActualLRPNetInfo{} actualLRP.CrashCount = newCrashCount actualLRP.CrashReason = crashReason evacuating := false if actualLRP.ShouldRestartImmediately(models.NewDefaultRestartCalculator()) { actualLRP.State = models.ActualLRPStateUnclaimed immediateRestart = true } now := db.clock.Now().UnixNano() actualLRP.Since = now _, err = db.update(logger, tx, actualLRPsTable, SQLAttributes{ "state": actualLRP.State, "cell_id": actualLRP.CellId, "instance_guid": actualLRP.InstanceGuid, "modification_tag_index": actualLRP.ModificationTag.Index, "crash_count": actualLRP.CrashCount, "crash_reason": truncateString(actualLRP.CrashReason, 1024), "since": actualLRP.Since, "net_info": []byte{}, }, "process_guid = ? AND instance_index = ? AND evacuating = ?", key.ProcessGuid, key.Index, evacuating, ) if err != nil { logger.Error("failed-to-crash-actual-lrp", err) return db.convertSQLError(err) } return nil }) return &models.ActualLRPGroup{Instance: &beforeActualLRP}, &models.ActualLRPGroup{Instance: actualLRP}, immediateRestart, err }
func (db *SQLDB) StartActualLRP(logger lager.Logger, key *models.ActualLRPKey, instanceKey *models.ActualLRPInstanceKey, netInfo *models.ActualLRPNetInfo) (*models.ActualLRPGroup, *models.ActualLRPGroup, error) { logger = logger.WithData(lager.Data{"actual_lrp_key": key, "actual_lrp_instance_key": instanceKey, "net_info": netInfo}) var beforeActualLRP models.ActualLRP var actualLRP *models.ActualLRP err := db.transact(logger, func(logger lager.Logger, tx *sql.Tx) error { var err error actualLRP, err = db.fetchActualLRPForUpdate(logger, key.ProcessGuid, key.Index, false, tx) if err == models.ErrResourceNotFound { actualLRP, err = db.createRunningActualLRP(logger, key, instanceKey, netInfo, tx) return err } if err != nil { logger.Error("failed-to-get-actual-lrp", err) return err } beforeActualLRP = *actualLRP if actualLRP.ActualLRPKey.Equal(key) && actualLRP.ActualLRPInstanceKey.Equal(instanceKey) && actualLRP.ActualLRPNetInfo.Equal(netInfo) && actualLRP.State == models.ActualLRPStateRunning { logger.Debug("nothing-to-change") return nil } if !actualLRP.AllowsTransitionTo(key, instanceKey, models.ActualLRPStateRunning) { logger.Error("failed-to-transition-actual-lrp-to-started", nil) return models.ErrActualLRPCannotBeStarted } logger.Info("starting") defer logger.Info("completed") now := db.clock.Now().UnixNano() evacuating := false actualLRP.ActualLRPInstanceKey = *instanceKey actualLRP.ActualLRPNetInfo = *netInfo actualLRP.State = models.ActualLRPStateRunning actualLRP.Since = now actualLRP.ModificationTag.Increment() actualLRP.PlacementError = "" netInfoData, err := db.serializeModel(logger, &actualLRP.ActualLRPNetInfo) if err != nil { logger.Error("failed-to-serialize-net-info", err) return err } _, err = db.update(logger, tx, actualLRPsTable, SQLAttributes{ "state": actualLRP.State, "cell_id": actualLRP.CellId, "instance_guid": actualLRP.InstanceGuid, "modification_tag_index": actualLRP.ModificationTag.Index, "placement_error": actualLRP.PlacementError, "since": actualLRP.Since, "net_info": netInfoData, }, "process_guid = ? AND instance_index = ? AND evacuating = ?", key.ProcessGuid, key.Index, evacuating, ) if err != nil { logger.Error("failed-starting-actual-lrp", err) return db.convertSQLError(err) } return nil }) return &models.ActualLRPGroup{Instance: &beforeActualLRP}, &models.ActualLRPGroup{Instance: actualLRP}, err }
BeforeEach(func() { before = &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("fake-process-guid", 1, "fake-domain"), } afterKey = models.ActualLRPKey{} afterKey = before.ActualLRPKey }) Context("when the ProcessGuid fields differ", func() { BeforeEach(func() { before.ProcessGuid = "some-process-guid" afterKey.ProcessGuid = "another-process-guid" }) It("is not allowed", func() { Expect(before.AllowsTransitionTo(&afterKey, &before.ActualLRPInstanceKey, before.GetState())).To(BeFalse()) }) }) Context("when the Index fields differ", func() { BeforeEach(func() { before.Index = 1138 afterKey.Index = 3417 }) It("is not allowed", func() { Expect(before.AllowsTransitionTo(&afterKey, &before.ActualLRPInstanceKey, before.GetState())).To(BeFalse()) }) }) Context("when the Domain fields differ", func() {