Search code examples
javagradleperforcesocketexceptionp4java

Gradle P4Java java.net.SocketTimeoutException: Read timed out


I am using P4Java library in my build.gradle file to sync a large zip file (>200MB) residing at a remote Perforce repository but I am encountering a "java.net.SocketTimeoutException: Read timed out" error either during the sync process or (mostly) during deleting the temporary client created for the sync operation. I am referring http://razgulyaev.blogspot.in/2011/08/p4-java-api-how-to-work-with-temporary.html for working with temporary clients using P4Java API.

I tried increasing the socket read timeout from default 30 sec as suggested in http://answers.perforce.com/articles/KB/8044 and also by introducing sleep but both approaches didn't solved the problem. Probing the server to verify the connection using getServerInfo() right before performing sync or delete operations results in a successful connection check. Can someone please point me as to where I should look for answers?

Thank you.

Providing the code snippet:

void perforceSync(String srcPath, String destPath, String server) {

    // Generating the file(s) to sync-up
    String[] pathUnderDepot = [
            srcPath + "*"
        ]

    // Increasing timeout from default 30 sec to 60 sec
    Properties defaultProps = new Properties()
    defaultProps.put(PropertyDefs.PROG_NAME_KEY, "CustomBuildApp")
    defaultProps.put(PropertyDefs.PROG_VERSION_KEY, "tv_1.0")
    defaultProps.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "60000")

    // Instantiating the server
    IOptionsServer p4Server = ServerFactory.getOptionsServer("p4java://" + server, defaultProps)
    p4Server.connect()

    // Authorizing
    p4Server.setUserName("perforceUserName")
    p4Server.login("perforcePassword")

    // Just check if connected successfully
    IServerInfo serverInfo = p4Server.getServerInfo()
    println 'Server info: ' + serverInfo.getServerLicense()

    // Creating new client
    IClient tempClient = new Client()

    // Setting up the name and the root folder
    tempClient.setName("tempClient" + UUID.randomUUID().toString().replace("-", ""))
    tempClient.setRoot(destPath)
    tempClient.setServer(p4Server)

    // Setting the client as the current one for the server
    p4Server.setCurrentClient(tempClient)

    // Creating Client View entry
    ClientViewMapping tempMappingEntry = new ClientViewMapping()

    // Setting up the mapping properties
    tempMappingEntry.setLeft(srcPath + "...")
    tempMappingEntry.setRight("//" + tempClient.getName() + "/...")
    tempMappingEntry.setType(EntryType.INCLUDE)

    // Creating Client view
    ClientView tempClientView = new ClientView()

    // Attaching client view entry to client view
    tempClientView.addEntry(tempMappingEntry)
    tempClient.setClientView(tempClientView)

    // Registering the new client on the server
    println p4Server.createClient(tempClient)

    // Surrounding the underlying block with try as we want some action
    // (namely client removing) to be performed in any way
    try {

        // Forming the FileSpec collection to be synced-up
        List<IFileSpec> fileSpecsSet = FileSpecBuilder.makeFileSpecList(pathUnderDepot)

        // Syncing up the client
        println "Syncing..."
        tempClient.sync(FileSpecBuilder.getValidFileSpecs(fileSpecsSet), true, false, false, false)
    }
    catch (Exception e) {
        println "Sync failed.  Trying again..."
        sleep(60 * 1000)
        tempClient.sync(FileSpecBuilder.getValidFileSpecs(fileSpecsSet), true, false, false, false)
    }
    finally {
        println "Done syncing."

        try {
            p4Server.connect()

            IServerInfo serverInfo2 = p4Server.getServerInfo()
            println '\nServer info: ' + serverInfo2.getServerLicense()

            // Removing the temporary client from the server
            println p4Server.deleteClient(tempClient.getName(), false)
        }
        catch(Exception e) {
            println 'Ignoring exception caught while deleting tempClient!'

            /*sleep(60 * 1000)
            p4Server.connect()

            IServerInfo serverInfo3 = p4Server.getServerInfo()
            println '\nServer info: ' + serverInfo3.getServerLicense()

            sleep(60 * 1000)
            println p4Server.deleteClient(tempClient.getName(), false)*/
        }
    }
}

One unusual thing which I observed while deleting tempClient was it was actually deleting the client but still throwing "java.net.SocketTimeoutException: Read timed out" which is why I ended up commenting the second delete attempt in the second catch block.


Solution

  • Which version of P4Java are you using? Have you tried this out with the newest P4Java? There are notable fixes dealing with RPC sockets since the 2013.2 version forward as can be seen in the release notes:

    http://www.perforce.com/perforce/doc.current/user/p4javanotes.txt
    

    Here are some variations that you can try where you have your code to increase timeout and instantiating the server:

    a] Have you tried to passing props in its own argument,? For example:

      Properties prop = new Properties();
      prop.setProperty(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "300000");
      UsageOptions uop = new UsageOptions(prop);
      server = ServerFactory.getOptionsServer(ServerFactory.DEFAULT_PROTOCOL_NAME + "://" + serverPort, prop, uop);
    

    Or something like the following:

      IOptionsServer p4Server = ServerFactory.getOptionsServer("p4java://" + server, defaultProps)
    

    You can also set the timeout to "0" to give it no timeout.

    b]

       props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "60000");
       props.put(RpcPropertyDefs.RPC_SOCKET_POOL_SIZE_NICK, "5");
    

    c]

    Properties props = System.getProperties();
    props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "60000");
    
    IOptionsServer server =
    ServerFactory.getOptionsServer("p4java://perforce:1666", props, null);
    

    d] In case you have Eclipse users using our P4Eclipse plugin, the property can be set in the plugin preferences (Team->Perforce->Advanced) under the Custom P4Java Properties.

    "sockSoTimeout" : "3000000"

    REFERENCES