/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.raptor.metadata;

import com.facebook.presto.raptor.util.DaoSupplier;

import javax.inject.Inject;

import java.util.UUID;

public class DatabaseShardRecorder
        implements ShardRecorder
{
    private final ShardDao dao;

    @Inject
    public DatabaseShardRecorder(DaoSupplier<ShardDao> shardDaoSupplier)
    {
        this.dao = shardDaoSupplier.onDemand();
    }

    @Override
    public void recordCreatedShard(long transactionId, UUID shardUuid)
    {
        dao.insertCreatedShard(shardUuid, transactionId);
    }
}

@@ -657,459 +657,1619 @@
 er;%0A
-%0Aimport javax.inject.Inject;%0A%0Aimport java.util.UUID;%0A%0Apublic class DatabaseShardRecorder%0A        implements ShardRecorder%0A%7B%0A    private final ShardDao dao;%0A%0A    @Inject%0A    public DatabaseShardRecorder(DaoSupplier%3CShardDao%3E shardDaoSupplier)%0A    %7B%0A        this.dao = shardDaoSupplier.onDemand();%0A    %7D%0A%0A    @Override%0A    public void recordCreatedShard(long transactionId, UUID shardUuid)%0A    %7B%0A        dao.insertCreatedShard(shardUuid, transactionId);
+import com.facebook.presto.spi.PrestoException;%0Aimport io.airlift.log.Logger;%0A%0Aimport javax.inject.Inject;%0A%0Aimport java.util.UUID;%0Aimport java.util.concurrent.ThreadLocalRandom;%0A%0Aimport static com.facebook.presto.raptor.util.DatabaseUtil.metadataError;%0Aimport static com.facebook.presto.raptor.util.DatabaseUtil.runIgnoringConstraintViolation;%0Aimport static java.util.concurrent.TimeUnit.MILLISECONDS;%0A%0Apublic class DatabaseShardRecorder%0A        implements ShardRecorder%0A%7B%0A    private static final Logger log = Logger.get(DatabaseShardRecorder.class);%0A%0A    private final ShardDao dao;%0A%0A    @Inject%0A    public DatabaseShardRecorder(DaoSupplier%3CShardDao%3E shardDaoSupplier)%0A    %7B%0A        this.dao = shardDaoSupplier.onDemand();%0A    %7D%0A%0A    @Override%0A    public void recordCreatedShard(long transactionId, UUID shardUuid)%0A    %7B%0A        int maxAttempts = 5;%0A        for (int attempt = 1; attempt %3C= maxAttempts; attempt++) %7B%0A            try %7B%0A                runIgnoringConstraintViolation(() -%3E dao.insertCreatedShard(shardUuid, transactionId));%0A            %7D%0A            catch (PrestoException e) %7B%0A                if (attempt == maxAttempts) %7B%0A                    throw e;%0A                %7D%0A                log.warn(e, %22Failed to insert created shard on attempt %25s, will retry%22, attempt);%0A                try %7B%0A                    long millis = attempt * 2000L;%0A                    MILLISECONDS.sleep(millis + ThreadLocalRandom.current().nextLong(0, millis));%0A                %7D%0A                catch (InterruptedException ie) %7B%0A                    throw metadataError(ie);%0A                %7D%0A            %7D%0A        %7D
 %0A   

Add retry for inserting created shards