From e635eb19fdbc009e46df0d450f30de4edcf35d45 Mon Sep 17 00:00:00 2001 From: Roberto Dip Date: Wed, 10 May 2023 09:40:11 -0300 Subject: [PATCH] use writer for database reads on TokenUpdate (#11605) Related to #11604 --- changes/11604-use-writer | 1 + server/datastore/mysql/apple_mdm.go | 3 ++- server/datastore/mysql/hosts.go | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 changes/11604-use-writer diff --git a/changes/11604-use-writer b/changes/11604-use-writer new file mode 100644 index 000000000..bb8e9a4f0 --- /dev/null +++ b/changes/11604-use-writer @@ -0,0 +1 @@ +* Fixed a bug that prevented bootstrap packages and the `fleetd` agent from being installed when the server had a database replica configured. diff --git a/server/datastore/mysql/apple_mdm.go b/server/datastore/mysql/apple_mdm.go index 248eb3e0d..f9d4f08e5 100644 --- a/server/datastore/mysql/apple_mdm.go +++ b/server/datastore/mysql/apple_mdm.go @@ -922,7 +922,8 @@ func unionSelectDevices(devices []godep.Device) (stmt string, args []interface{} func (ds *Datastore) GetNanoMDMEnrollment(ctx context.Context, id string) (*fleet.NanoEnrollment, error) { var nanoEnroll fleet.NanoEnrollment - err := sqlx.GetContext(ctx, ds.reader, &nanoEnroll, `SELECT id, device_id, type, enabled, token_update_tally + // use writer as it is used just after creation in some cases + err := sqlx.GetContext(ctx, ds.writer, &nanoEnroll, `SELECT id, device_id, type, enabled, token_update_tally FROM nano_enrollments WHERE id = ?`, id) if err != nil { if errors.Is(err, sql.ErrNoRows) { diff --git a/server/datastore/mysql/hosts.go b/server/datastore/mysql/hosts.go index 5f177b76b..28e6f47ea 100644 --- a/server/datastore/mysql/hosts.go +++ b/server/datastore/mysql/hosts.go @@ -2803,7 +2803,9 @@ func (ds *Datastore) GetHostMDM(ctx context.Context, hostID uint) (*fleet.HostMD func (ds *Datastore) GetHostMDMCheckinInfo(ctx context.Context, hostUUID string) (*fleet.HostMDMCheckinInfo, error) { var hmdm fleet.HostMDMCheckinInfo - err := sqlx.GetContext(ctx, ds.reader, &hmdm, ` + + // use writer as it is used just after creation in some cases + err := sqlx.GetContext(ctx, ds.writer, &hmdm, ` SELECT h.hardware_serial, COALESCE(hm.installed_from_dep, false) as installed_from_dep, @@ -2820,9 +2822,9 @@ func (ds *Datastore) GetHostMDMCheckinInfo(ctx context.Context, hostUUID string) WHERE h.uuid = ? LIMIT 1`, hostUUID) if err != nil { if err == sql.ErrNoRows { - return nil, ctxerr.Wrap(ctx, notFound("MDM").WithMessage(hostUUID)) + return nil, ctxerr.Wrap(ctx, notFound("Host").WithMessage(fmt.Sprintf("with UUID: %s", hostUUID))) } - return nil, ctxerr.Wrapf(ctx, err, "getting data from host_mdm for host_uuid %s", hostUUID) + return nil, ctxerr.Wrapf(ctx, err, "host mdm checkin info for host UUID %s", hostUUID) } return &hmdm, nil }