Azure Functions migrated to v 4.x, increased Azure Files transaction count

115 Views Asked by At

My environment: Azure Functions App with enabled 2 TimeTrigger Functions (triggered 1-2 times per day for a few minutes). Powershell Core, Windows OS, consumption plan. Functions are used to scrape data from websites.

Pseudocode:

Invoke-webrequest -uri XXX | Convert-FromJson
foreach (){
data += element
} 
send-data

few weeks ago I've migrated Azure Functions to v. 4. Yesterday I have checked Insights of the connected storage account and saw, that after migration file transactions count increased almost 5-6x times.

Azure Files Insights screenshot

So now a question, maybe someone aware what happened to Azure Function App in v4.x, why there are so many transactions compared to v2.x / v3.x?

What did you try: First thought was there is some problem with migrated instance, but nope. I created a fresh Azure Function App (Powershell core, consumption plan, but now a Linux OS) but the situation hasn't changed, also some random spikes and non-stop create-close AzureFiles transactions happen.

New Azure Files Insights screenshot

UPD. Code provided. Send-TelegramTextMessage - Poshgram module (https://github.com/techthoughts2/PoshGram)

param($Timer)

$currentUTCtime = (Get-Date).ToUniversalTime()

if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}
$ID1 = "XXX"
$ID2 = "YYY"

$botToken = "XXXX"
$weekNR = get-date -UFormat %V
$year = get-date -UFormat %Y

$ParseFrom = Invoke-WebRequest "example.com"

$UpcomingFree = $ParseFrom.ToString() -split {$_ -eq ">" -or $_ -eq "<"} | Select-String -Pattern 'title="Free' 

$UF1 = $UpcomingFree | Select-Object -First 1
$UpcomingFree1 = $UF1 -split '"' | Select-String -Pattern 'Free ' | Out-String

$AddAvalableNow = $ParseFrom.ToString() -split {$_ -eq ">" -or $_ -eq "<"} | Select-String -Pattern 'Available Now' 
$AddAvalableNow -split ' &amp; ' -join " & " 

#============GET WebSiteName DATA================

$getWebSiteNameJson = Invoke-WebRequest "example2.com/json"
$WebSiteNameJson = $getWebSiteNameJson.Content | ConvertFrom-Json
$WebSiteNameJsonElements= $WebSiteNameJson.data.Catalog.searchStore.elements

$nnn = Get-Date

foreach ($WebSiteNameJsonElement in $WebSiteNameJsonElements) {
    $promLen = $WebSiteNameJsonElement.promotions.promotionalOffers.promotionalOffers
    $promLenUpc = $WebSiteNameJsonElement.promotions.upcomingPromotionalOffers.promotionalOffers

    if ($promLen.Count -gt 0){
        foreach ($promLenItem in $promLen)
        {
            if (($promLenItem.endDate - $promLenItem.startDate).Days -le 7 -and $promLenItem.discountSetting.discountPercentage -eq 0 -and (($nnn -ge $promLenItem.startDate) -and ($nnn -le $promLenItem.endDate)) ) {
                $Available += "`n- "+$WebSiteNameJsonElement.title + "; "
            }
        }
    }
    if ($promLenUpc.Count -gt 0){
        foreach ($promLenItemUpc in $promLenUpc)
        {
            if (($promLenItemUpc.endDate - $promLenItemUpc.startDate).Days -le 7 -and $promLenItemUpc.discountSetting.discountPercentage -eq 0 -and (($nnn.AddDays(7) -ge $promLenItemUpc.startDate) -and ($nnn.AddDays(7) -le $promLenItemUpc.endDate))) {
                $coming += "`n- "+$WebSiteNameJsonElement.title + "; "
            }
        }
    }

} 
$coming1 += " `n$coming" 
$Available1 += "`n$Available"

$today = (Get-Date).DayOfWeek.value__

if ($today -lt 4){
    $daysleft = 4-$today
    $daysleftMessage = "`n$daysleft"
} elseif ($today -gt 4) {
    $daysleft = 12-$today
    $daysleftMessage = "`n$daysleft"
}
$messageHeader = "$weekNR; $year"
$messageWebSiteNameBody = "`n$Available1 `n`n$coming1"
$oldMessageBody = "$UpcomingFree1"

$sendchanel = Send-TelegramTextMessage -BotToken $botToken -ChatID $ID2 -Message "$messageHeader `n`n$messageWebSiteNameBody`n`n `n$daysleftMessage"
$messageSendChat = $sendchanel.result.message_id
$send = Send-TelegramTextMessage -BotToken $botToken -ChatID $ID1 -Message "$messageSendChat `n`n$oldMessageBody"

Clear-Variable coming, coming1, Available, Available1

UPD2. 2 weeks ago second function was disable, so only one running now. Also timer schedule was updated to: "schedule": "0 0 17 * * 1,3,4,6", so it is triggered only 4 times a week at 18:00 CET (17:00 UTC), screenshot as a proof. Function Invocations But again after checking storage account insights there is a HUGE spike which wasn't triggered by my function SA spike

1

There are 1 best solutions below

2
On

I tried with the below code that does the functionality of fetching the API response and sending them to the text files stored in the blob storage.

param($Timer)
$currentUTCtime  = (Get-Date).ToUniversalTime()

if ($Timer.IsPastDue) {
Write-Host  "PowerShell timer is running late!"
}
  

$apiUrl  =  "https://randomuser.me/api/"
$timestamp  =  Get-Date  -Format "yyyyMMddHHmmss"  

Connect-AzAccount  -Tenant '<tenant_id>'  -SubscriptionId '<subscription_id>'
$resourceGroup=  "DefaultResourceGroup-CID"
$storageAccountName  =  "kamstorage101azfps"
$containerName  =  "apidatactr"
$blobName  =  "response_$timestamp.txt"
# $localFilePath = "C:\Users\kamalid\source\repos\Kamali\ResponseFiles\$blobName"
$localFilePath  =  "https://kamstorage101azfps.file.core.windows.net/kamfunpsapp101baf0/apiresponsefiles/$blobName"


$response  =  Invoke-WebRequest  -Uri $apiUrl  -Method Get
 
if ($response.StatusCode  -eq  200) {
$response.Content  |  Set-Content  -Path $localFilePath 

Write-Host  "Response from the API is saved in the text file"
} else {
Write-Host  "Request failed with status code: $($response.StatusCode)"
}

  

if (-not  $script:storageContext) {

$storageContext  =  New-AzStorageContext  -StorageAccountName $storageAccountName  -StorageAccountKey (Get-AzStorageAccountKey  -ResourceGroupName $resourceGroup  -Name $storageAccountName).Value[0]
Set-AzStorageBlobContent  -Container $containerName  -File $localFilePath  -Blob $blobName  -BlobType Block -Context $storageContext  -Force

}

$storageContext  =  New-AzStorageContext  -StorageAccountName $storageAccountName  -StorageAccountKey (Get-AzStorageAccountKey  -ResourceGroupName $resourceGroup  -Name $storageAccountName).Value[0]
Set-AzStorageBlobContent  -Container $containerName  -File $localFilePath  -Blob $blobName  -BlobType Block -Context $storageContext  -Force


Write-Host  "PowerShell timer trigger function ran! TIME: $currentUTCtime"

I have used the Azure Functions v4 and observed the transactions of Timer trigger with the above code in both Windows and Linux OS with the few minutes, and small spikes present in transactions.

enter image description here

When deployed to azure function app:

enter image description here

Running on a Linux OS can have different file systems, file access patterns, and resource management behaviors compared to Windows.

Scaling and resource Utilization are improved and the runtime is changed for the v4 Azure Functions i.e., Dapr runtime.

As you didn’t give the full code in the question, I’m assuming the libraries or dependencies are different compared to v3 and v3 which might result in different file access patterns, leading to increased file transactions as shown in the Microsoft Doc reference.