+ if (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
+ Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
+ goto bail_out;
+ }
+ if (ctx.count == 0) {
+ Jmsg(jcr, M_INFO, 0, _("No Volumes found to migrate.\n"));
+ goto ok_out;
+ }
+ pool_bytes = ctx.value;
+ Dmsg2(dbglevel, "highbytes=%d pool=%d\n", (int)jcr->pool->MigrationHighBytes,
+ (int)pool_bytes);
+ if (pool_bytes < (int64_t)jcr->pool->MigrationHighBytes) {
+ Jmsg(jcr, M_INFO, 0, _("No Volumes found to migrate.\n"));
+ goto ok_out;
+ }
+ Dmsg0(dbglevel, "We should do Occupation migration.\n");
+
+ ids.count = 0;
+ /* Find a list of MediaIds that could be migrated */
+ Mmsg(query, sql_mediaids, jcr->pool->hdr.name);
+// Dmsg1(dbglevel, "query=%s\n", query.c_str());
+ if (!db_sql_query(jcr->db, query.c_str(), dbid_handler, (void *)&ids)) {
+ Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
+ goto bail_out;
+ }
+ if (ids.count == 0) {
+ Jmsg(jcr, M_INFO, 0, _("No Volumes found to migrate.\n"));
+ goto ok_out;
+ }
+ Dmsg2(dbglevel, "Pool Occupancy ids=%d MediaIds=%s\n", ids.count, ids.list);
+
+ /*
+ * Now loop over MediaIds getting more JobIds to migrate until
+ * we reduce the pool occupancy below the low water mark.
+ */
+ p = ids.list;
+ for (int i=0; i < (int)ids.count; i++) {
+ stat = get_next_dbid_from_list(&p, &MediaId);
+ Dmsg2(dbglevel, "get_next_dbid stat=%d MediaId=%u\n", stat, MediaId);
+ if (stat < 0) {
+ Jmsg(jcr, M_FATAL, 0, _("Invalid MediaId found.\n"));
+ goto bail_out;
+ } else if (stat == 0) {
+ break;
+ }
+ mid.count = 1;
+ Mmsg(mid.list, "%s", edit_int64(MediaId, ed1));
+ ok = find_jobids_from_mediaid_list(jcr, &mid, "Volumes");
+ if (!ok) {
+ continue;
+ }
+ if (i != 0) {
+ pm_strcat(jids.list, ",");
+ }
+ pm_strcat(jids.list, mid.list);
+ jids.count += mid.count;
+
+ /* Now get the count of bytes added */
+ ctx.count = 0;
+ /* Find count of bytes from Jobs */
+ Mmsg(query, sql_job_bytes, mid.list);
+ if (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
+ Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
+ goto bail_out;
+ }
+ pool_bytes -= ctx.value;
+ Dmsg1(dbglevel, "Job bytes=%d\n", (int)ctx.value);
+ Dmsg2(dbglevel, "lowbytes=%d pool=%d\n", (int)jcr->pool->MigrationLowBytes,
+ (int)pool_bytes);
+ if (pool_bytes <= (int64_t)jcr->pool->MigrationLowBytes) {
+ Dmsg0(dbglevel, "We should be done.\n");
+ break;
+ }
+
+ }
+ Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", jids.count, jids.list);
+