+ ctx.count = 0;
+ /* Find count of bytes in pool */
+ Mmsg(query, sql_pool_bytes, jcr->rpool->name());
+ 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 %s.\n"), jcr->get_ActionName(0));
+ goto ok_out;
+ }
+ pool_bytes = ctx.value;
+ Dmsg2(dbglevel, "highbytes=%lld pool=%lld\n", jcr->rpool->MigrationHighBytes,
+ pool_bytes);
+ if (pool_bytes < (int64_t)jcr->rpool->MigrationHighBytes) {
+ Jmsg(jcr, M_INFO, 0, _("No Volumes found to %s.\n"), jcr->get_ActionName(0));
+ 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->rpool->name());
+ Dmsg1(dbglevel, "query=%s\n", query.c_str());
+ if (!db_sql_query(jcr->db, query.c_str(), unique_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 %s.\n"), jcr->get_ActionName(0));
+ goto ok_out;
+ }
+ Dmsg2(dbglevel, "Pool Occupancy ids=%d MediaIds=%s\n", ids.count, ids.list);
+
+ if (!find_jobids_from_mediaid_list(jcr, &ids, "Volume")) {
+ goto bail_out;
+ }
+ /* ids == list of jobs */
+ p = ids.list;
+ for (int i=0; i < (int)ids.count; i++) {
+ stat = get_next_dbid_from_list(&p, &DBId);
+ Dmsg2(dbglevel, "get_next_dbid stat=%d JobId=%u\n", stat, (uint32_t)DBId);
+ if (stat < 0) {
+ Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
+ goto bail_out;
+ } else if (stat == 0) {
+ break;
+ }
+
+ mid.count = 1;
+ Mmsg(mid.list, "%s", edit_int64(DBId, ed1));
+ if (jids.count > 0) {
+ pm_strcat(jids.list, ",");
+ }
+ pm_strcat(jids.list, mid.list);
+ jids.count += mid.count;
+
+ /* Find count of bytes from Jobs */
+ Mmsg(query, sql_job_bytes, mid.list);
+ Dmsg1(dbglevel, "Jobbytes query: %s\n", query.c_str());
+ 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;
+ Dmsg2(dbglevel, "Total %s Job bytes=%s\n", jcr->get_ActionName(0), edit_int64_with_commas(ctx.value, ed1));
+ Dmsg2(dbglevel, "lowbytes=%s poolafter=%s\n",
+ edit_int64_with_commas(jcr->rpool->MigrationLowBytes, ed1),
+ edit_int64_with_commas(pool_bytes, ed2));
+ if (pool_bytes <= (int64_t)jcr->rpool->MigrationLowBytes) {
+ Dmsg0(dbglevel, "We should be done.\n");
+ break;
+ }
+ }
+ /* Transfer jids to ids, where the jobs list is expected */
+ ids.count = jids.count;
+ pm_strcpy(ids.list, jids.list);
+ Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", ids.count, ids.list);