Search in sources :

Example 11 with SuppressForbidden

use of org.apache.solr.common.util.SuppressForbidden in project lucene-solr by apache.

the class AbstractSqlEntityProcessorTestCase method modifySomeCountries.

@SuppressForbidden(reason = "Needs currentTimeMillis to set change time for SQL query")
public String[] modifySomeCountries() throws Exception {
    underlyingDataModified = true;
    int numberToChange = random().nextInt(countries.length + 1);
    Set<String> changeSet = new HashSet<>();
    Connection conn = null;
    PreparedStatement change = null;
    // One second in the future ensures a change time after the last import (DIH
    // uses second precision only)
    Timestamp theTime = new Timestamp(System.currentTimeMillis() + 1000);
    log.debug("COUNTRY UPDATE USING TIMESTAMP: " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ROOT).format(theTime));
    try {
        conn = newConnection();
        change = conn.prepareStatement("update countries set country_name=?, last_modified=? where code=?");
        for (int i = 0; i < numberToChange; i++) {
            int tryIndex = random().nextInt(countries.length);
            String code = countries[tryIndex][0];
            if (!changeSet.contains(code)) {
                changeSet.add(code);
                change.setString(1, "MODIFIED " + countries[tryIndex][1]);
                change.setTimestamp(2, theTime);
                change.setString(3, code);
                Assert.assertEquals(1, change.executeUpdate());
            }
        }
    } catch (SQLException e) {
        throw e;
    } finally {
        try {
            change.close();
        } catch (Exception ex) {
        }
        try {
            conn.close();
        } catch (Exception ex) {
        }
    }
    return changeSet.toArray(new String[changeSet.size()]);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) SimpleDateFormat(java.text.SimpleDateFormat) SQLException(java.sql.SQLException) HashSet(java.util.HashSet) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 12 with SuppressForbidden

use of org.apache.solr.common.util.SuppressForbidden in project lucene-solr by apache.

the class TestInjection method waitForInSyncWithLeader.

@SuppressForbidden(reason = "Need currentTimeMillis, because COMMIT_TIME_MSEC_KEY use currentTimeMillis as value")
public static boolean waitForInSyncWithLeader(SolrCore core, ZkController zkController, String collection, String shardId) throws InterruptedException {
    if (waitForReplicasInSync == null)
        return true;
    log.info("Start waiting for replica in sync with leader");
    long currentTime = System.currentTimeMillis();
    Pair<Boolean, Integer> pair = parseValue(waitForReplicasInSync);
    boolean enabled = pair.first();
    if (!enabled)
        return true;
    long t = System.currentTimeMillis() - 200;
    try {
        for (int i = 0; i < pair.second(); i++) {
            if (core.isClosed())
                return true;
            Replica leaderReplica = zkController.getZkStateReader().getLeaderRetry(collection, shardId);
            try (HttpSolrClient leaderClient = new HttpSolrClient.Builder(leaderReplica.getCoreUrl()).build()) {
                ModifiableSolrParams params = new ModifiableSolrParams();
                params.set(CommonParams.QT, ReplicationHandler.PATH);
                params.set(COMMAND, CMD_DETAILS);
                NamedList<Object> response = leaderClient.request(new QueryRequest(params));
                long leaderVersion = (long) ((NamedList) response.get("details")).get("indexVersion");
                RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
                try {
                    String localVersion = searcher.get().getIndexReader().getIndexCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
                    if (localVersion == null && leaderVersion == 0 && !core.getUpdateHandler().getUpdateLog().hasUncommittedChanges())
                        return true;
                    if (localVersion != null && Long.parseLong(localVersion) == leaderVersion && (leaderVersion >= t || i >= 6)) {
                        log.info("Waiting time for tlog replica to be in sync with leader: {}", System.currentTimeMillis() - currentTime);
                        return true;
                    } else {
                        log.debug("Tlog replica not in sync with leader yet. Attempt: {}. Local Version={}, leader Version={}", i, localVersion, leaderVersion);
                        Thread.sleep(500);
                    }
                } finally {
                    searcher.decref();
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception when wait for replicas in sync with master");
    }
    return false;
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Replica(org.apache.solr.common.cloud.Replica) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException) NonExistentCoreException(org.apache.solr.common.NonExistentCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 13 with SuppressForbidden

use of org.apache.solr.common.util.SuppressForbidden in project lucene-solr by apache.

the class AbstractSqlEntityProcessorTestCase method populateData.

@SuppressForbidden(reason = "Needs currentTimeMillis to set change time for SQL query")
@Override
protected void populateData(Connection conn) throws Exception {
    Statement s = null;
    PreparedStatement ps = null;
    // 10 seconds ago
    Timestamp theTime = new Timestamp(System.currentTimeMillis() - 10000);
    try {
        s = conn.createStatement();
        s.executeUpdate("create table countries(code varchar(3) not null primary key, country_name varchar(50), deleted char(1) default 'N', last_modified timestamp not null)");
        s.executeUpdate("create table people(id int not null primary key, name varchar(50), country_code char(2), deleted char(1) default 'N', last_modified timestamp not null)");
        s.executeUpdate("create table people_sports(id int not null primary key, person_id int, sport_name varchar(50), deleted char(1) default 'N', last_modified timestamp not null)");
        log.debug("INSERTING DB DATA USING TIMESTAMP: " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ROOT).format(theTime));
        ps = conn.prepareStatement("insert into countries (code, country_name, last_modified) values (?,?,?)");
        for (String[] country : countries) {
            ps.setString(1, country[0]);
            ps.setString(2, country[1]);
            ps.setTimestamp(3, theTime);
            Assert.assertEquals(1, ps.executeUpdate());
        }
        ps.close();
        ps = conn.prepareStatement("insert into people (id, name, country_code, last_modified) values (?,?,?,?)");
        for (Object[] person : people) {
            ps.setInt(1, (Integer) person[0]);
            ps.setString(2, (String) person[1]);
            ps.setString(3, (String) person[2]);
            ps.setTimestamp(4, theTime);
            Assert.assertEquals(1, ps.executeUpdate());
        }
        ps.close();
        ps = conn.prepareStatement("insert into people_sports (id, person_id, sport_name, last_modified) values (?,?,?,?)");
        for (Object[] sport : people_sports) {
            ps.setInt(1, (Integer) sport[0]);
            ps.setInt(2, (Integer) sport[1]);
            ps.setString(3, (String) sport[2]);
            ps.setTimestamp(4, theTime);
            Assert.assertEquals(1, ps.executeUpdate());
        }
        ps.close();
        conn.commit();
        conn.close();
    } catch (Exception e) {
        throw e;
    } finally {
        try {
            ps.close();
        } catch (Exception ex) {
        }
        try {
            s.close();
        } catch (Exception ex) {
        }
        try {
            conn.close();
        } catch (Exception ex) {
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) SimpleDateFormat(java.text.SimpleDateFormat) SQLException(java.sql.SQLException) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 14 with SuppressForbidden

use of org.apache.solr.common.util.SuppressForbidden in project lucene-solr by apache.

the class TestFileListEntityProcessor method testNTOT.

@SuppressForbidden(reason = "Needs currentTimeMillis to set last modified time")
@Test
public void testNTOT() throws IOException {
    File tmpdir = createTempDir().toFile();
    createFile(tmpdir, "a.xml", "a.xml".getBytes(StandardCharsets.UTF_8), true);
    createFile(tmpdir, "b.xml", "b.xml".getBytes(StandardCharsets.UTF_8), true);
    createFile(tmpdir, "c.props", "c.props".getBytes(StandardCharsets.UTF_8), true);
    Map attrs = createMap(FileListEntityProcessor.FILE_NAME, "xml$", FileListEntityProcessor.BASE_DIR, tmpdir.getAbsolutePath(), FileListEntityProcessor.OLDER_THAN, "'NOW'");
    List<String> fList = getFiles(null, attrs);
    assertEquals(2, fList.size());
    attrs = createMap(FileListEntityProcessor.FILE_NAME, ".xml$", FileListEntityProcessor.BASE_DIR, tmpdir.getAbsolutePath(), FileListEntityProcessor.NEWER_THAN, "'NOW-2HOURS'");
    fList = getFiles(null, attrs);
    assertEquals(2, fList.size());
    // Use a variable for newerThan
    attrs = createMap(FileListEntityProcessor.FILE_NAME, ".xml$", FileListEntityProcessor.BASE_DIR, tmpdir.getAbsolutePath(), FileListEntityProcessor.NEWER_THAN, "${a.x}");
    VariableResolver resolver = new VariableResolver();
    String lastMod = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT).format(new Date(System.currentTimeMillis() - 50000));
    resolver.addNamespace("a", createMap("x", lastMod));
    createFile(tmpdir, "t.xml", "t.xml".getBytes(StandardCharsets.UTF_8), false);
    fList = getFiles(resolver, attrs);
    assertEquals(1, fList.size());
    assertEquals("File name must be t.xml", new File(tmpdir, "t.xml").getAbsolutePath(), fList.get(0));
}
Also used : File(java.io.File) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 15 with SuppressForbidden

use of org.apache.solr.common.util.SuppressForbidden in project lucene-solr by apache.

the class IndexFetcher method markReplicationStart.

@SuppressForbidden(reason = "Need currentTimeMillis for debugging/stats")
private void markReplicationStart() {
    replicationTimer = new RTimer();
    replicationStartTimeStamp = new Date();
}
Also used : RTimer(org.apache.solr.util.RTimer) Date(java.util.Date) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Aggregations

SuppressForbidden (org.apache.solr.common.util.SuppressForbidden)15 SimpleDateFormat (java.text.SimpleDateFormat)6 Date (java.util.Date)6 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 Timestamp (java.sql.Timestamp)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 File (java.io.File)2 Principal (java.security.Principal)2 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Header (org.apache.http.Header)2 BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)2 SolrException (org.apache.solr.common.SolrException)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1