Hi everyone :slightly_smiling_face: Recently I sta...
# server
l
Hi everyone 🙂 Recently I started playing around with Google Cloud Functions/MongoDB Atlas with the JVM runtime in Kotlin. I've found that when I test locally, during cold start the connection time to Atlas is comparable with the Node runtime, but when connecting from GC, during cold start it can take up to 10 seconds to make a connection! Below I've added my Gradle config and connection code. I'm really hoping someone can spot what is wrong to help me speed up the connection time. Gradle:
Copy code
buildscript {
    ext.kotlin_version = '1.4.20'

    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.github.jengelman.gradle.plugins:shadow:5.0.0"
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

apply plugin: 'com.github.johnrengelman.shadow'
tasks.build.dependsOn tasks.shadowJar

shadowJar {
    mergeServiceFiles()
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    jcenter()
    mavenCentral()
}

compileKotlin {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

configurations {
    invoker
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    // Every function needs this dependency to get the Functions Framework API.
    compileOnly 'com.google.cloud.functions:functions-framework-api:1.0.1'

    // MongoDB & KMongo
    implementation "org.mongodb:mongodb-driver-sync:4.1.1"
    implementation 'org.mongodb:mongodb-driver-core:4.1.1'
    implementation 'org.mongodb:bson:4.1.1'
    implementation 'org.mongodb:mongo-java-driver:3.12.7'
    implementation 'org.litote.kmongo:kmongo-coroutine:4.2.0'

    implementation "org.slf4j:slf4j-simple:1.7.30"
}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
    test.java.srcDirs += 'src/test/kotlin'
}
Connection code:
Copy code
import com.mongodb.MongoClientURI
import functions.Conf.Conf.MONGO_CLIENT_URI
import org.litote.kmongo.coroutine.coroutine
import org.litote.kmongo.reactivestreams.KMongo

object Database {

    init {
        System.setProperty(
            "org.litote.mongo.test.mapping.service",
            "org.litote.kmongo.jackson.JacksonClassMappingTypeService"
        )
    }

    // get URI for MongoDB client (eg. Mongo Atlas)
    private val uri = MongoClientURI(MONGO_CLIENT_URI)

    // get client
    private val mongoClient = KMongo.createClient(uri.uri)

    // get database
    val database = mongoClient.getDatabase(uri.database).coroutine
}
k
This is why JVM and cloud functions (aka server less) makes no sense. Look at graalvm frameworks...
👍🏼 1
l
Can you explain why connecting to MongoDB Atlas with the JVM runtime is so much slower than Node? It only takes about 1 second longer than Node to spin up the function instance without connecting to Atlas, so was really hoping there would be a way to fix the above problem. Will have a look at graalvm though 🙂
k
JVM spinup is what hurts I think. Try adding trace timings in your app to see whether connection time is the real culprit...
l
I was checking the logs on the GC platform. The test function I was using was very small so spin up was +- 3 secs, then connection time was +- 10 secs 😞
k
Ok... That's bizarre...pester the Google support folks...
l
I'll do that 🙂 Thank you for your help!!