Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase:flatter:稳定性优化:多事务vs.批写入vs.集调用_Firebase_Flutter_Google Cloud Functions - Fatal编程技术网

Firebase:flatter:稳定性优化:多事务vs.批写入vs.集调用

Firebase:flatter:稳定性优化:多事务vs.批写入vs.集调用,firebase,flutter,google-cloud-functions,Firebase,Flutter,Google Cloud Functions,几个月前,这里的一个光荣的灵魂教会了我交易。我可能有点过火了,以为它们是自切片面包以来最好的东西。他们解决的问题是显而易见的,保证在单个文档上并发写入。然而,我已经注意到,我只使用了三个时间紧的函数触发器,就产生了可怕的结果:---------------------------“10中止:这些文档上的争用太多了。”------------------------- 为了稳定而优化,我的问题是:在不同的情况下使用这些写调用的混合包是否是最佳实践?例如:如果一个云函数正在写入一个我不希望出现争用的

几个月前,这里的一个光荣的灵魂教会了我交易。我可能有点过火了,以为它们是自切片面包以来最好的东西。他们解决的问题是显而易见的,保证在单个文档上并发写入。然而,我已经注意到,我只使用了三个时间紧的函数触发器,就产生了可怕的结果:

---------------------------“10中止:这些文档上的争用太多了。”-------------------------

为了稳定而优化,我的问题是:在不同的情况下使用这些写调用的混合包是否是最佳实践?例如:如果一个云函数正在写入一个我不希望出现争用的位置,那么它应该只是一个set调用吗?我应该使用批处理,而不是将4个事务处理发送到不同的位置吗

阅读Firebase限制时,我假设我的最大功率为60w/doc/sec。然而,我现在了解到,事务可以超时,并且只尝试写入5次

应用程序的一些背景信息和争用错误:
-这是一个基本的社交媒体应用程序。
-争用错误来自单个用户连续发布三篇文章。
-每个帖子都会触发一个云函数,该函数执行多个事务以将帖子链接到适当的位置。i、 e.关注者、提要、组、发送通知,并为每个关注者设置活动提要文档

附带问题:我是否错误地理解firebase可以处理具有这种活动级别的应用程序

编辑:我很早就意识到firebase的这些限制,并尽我最大的努力使文档和集合适当地分开

代码编辑:index.js:adminPostReview是抛出错误的特定函数(尽我所能简化)。
我认为引发错误的特定事务是对TransactionDayIndexAddd()的调用


我能够通过将TransactionDayIndexAddd()拆分为两个单独的事务来解决争用问题。我翻转一个bool来决定第二个是否应该运行

这使我相信嵌套的t.get/t.set事务会显著增加争用问题的发生几率。自从拆分以来,我一直无法重现错误。下面是新的TransactionDayIndexAddd(),供好奇的人使用

然而,我最初的问题仍然是关于稳定性的优化

async function transactionDayIndexAdd(docRef, dayPosted, postId, userId) {
    var dayAdd = 0;
    var promises = [];
    await db.runTransaction(async (t) => {
        var postMap = {};
        const doc = await t.get(docRef.doc(dayPosted));
        if (doc.exists)
            postMap = doc.data().pids;
        else {
            dayAdd = 1;
        }
        postMap[postId] = userId;
        t.set(doc.ref, { 'pids': postMap });
    });
    if (dayAdd == 1) {
        promises.push(db.runTransaction(async (t) => {
            const indexDoc = await t.get(docRef.doc('index'));
            var newIndex = indexDoc.exists ? indexDoc.data().index : {};
            newIndex[dayPosted] = true;
            t.set(indexDoc.ref, { 'index': newIndex });
        }));
    }
    return await Promise.all(promises);
}

由于我们看不到您的代码,也无法理解您的事务是如何工作的,所以很难说。请编辑问题以显示您有太多争用的特定代码,并解释特定用例。我想避免代码细节。但总是很乐意分享代码。谢谢。在堆栈溢出时,共享代码是默认的期望。这很有意义,哈哈。我将确保在将来的问题上有所改进。好的,我可以确认TransactionDayIndexAddd()中嵌套了t.get和t.set的事务导致了问题。我用布尔值将事务一分为二,以确定是否应该运行第二个事务。我能够连续发布5次,没有任何问题。我想嵌套t.get和t.set会使事务以指数方式不太可能成功?将继续测试一组更大的帖子。
async function transactionDayIndexAdd(docRef, dayPosted, postId, userId) {
    var dayAdd = 0;
    var promises = [];
    await db.runTransaction(async (t) => {
        var postMap = {};
        const doc = await t.get(docRef.doc(dayPosted));
        if (doc.exists)
            postMap = doc.data().pids;
        else {
            dayAdd = 1;
        }
        postMap[postId] = userId;
        t.set(doc.ref, { 'pids': postMap });
    });
    if (dayAdd == 1) {
        promises.push(db.runTransaction(async (t) => {
            const indexDoc = await t.get(docRef.doc('index'));
            var newIndex = indexDoc.exists ? indexDoc.data().index : {};
            newIndex[dayPosted] = true;
            t.set(indexDoc.ref, { 'index': newIndex });
        }));
    }
    return await Promise.all(promises);
}